容器视图之上的 UIView
UIView on top of a Container View
是否可以在容器视图之上显示一个 UIView?
我想添加一个带有一些不透明背景的视图,以便仍然可以看到我的容器视图。但是我尝试的一切都使我的 containerView 完全消失或在我的视图之上。我尝试通过 Storyboard 和代码。
我确定我遗漏了什么。
只需将您的视图添加到容器控制器容器
的视图属性
简单:
let viewYouWantToAddSubviewTo = parent?.view
详情:
import UIKit
class CustomNavigationViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
let layout = UICollectionViewFlowLayout()
let rootVC = HomeCollectionViewController(collectionViewLayout: layout)
viewControllers = [rootVC]
let v = UIView()
v.backgroundColor = UIColor.blue
v.layer.opacity = 0.4
v.translatesAutoresizingMaskIntoConstraints = false
// add your view to this view of the controller's container
let vv = (parent?.view)!
vv.addSubview(v)
// constraints for v
v.leftAnchor.constraint(equalTo: vv.leftAnchor).isActive = true
v.rightAnchor.constraint(equalTo: vv.rightAnchor).isActive = true
v.topAnchor.constraint(equalTo: vv.topAnchor).isActive = true
v.bottomAnchor.constraint(equalTo: vv.bottomAnchor).isActive = true
}
}
结果:
是否可以在容器视图之上显示一个 UIView?
我想添加一个带有一些不透明背景的视图,以便仍然可以看到我的容器视图。但是我尝试的一切都使我的 containerView 完全消失或在我的视图之上。我尝试通过 Storyboard 和代码。
我确定我遗漏了什么。
只需将您的视图添加到容器控制器容器
的视图属性简单:
let viewYouWantToAddSubviewTo = parent?.view
详情:
import UIKit
class CustomNavigationViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
let layout = UICollectionViewFlowLayout()
let rootVC = HomeCollectionViewController(collectionViewLayout: layout)
viewControllers = [rootVC]
let v = UIView()
v.backgroundColor = UIColor.blue
v.layer.opacity = 0.4
v.translatesAutoresizingMaskIntoConstraints = false
// add your view to this view of the controller's container
let vv = (parent?.view)!
vv.addSubview(v)
// constraints for v
v.leftAnchor.constraint(equalTo: vv.leftAnchor).isActive = true
v.rightAnchor.constraint(equalTo: vv.rightAnchor).isActive = true
v.topAnchor.constraint(equalTo: vv.topAnchor).isActive = true
v.bottomAnchor.constraint(equalTo: vv.bottomAnchor).isActive = true
}
}
结果: