我的应用中有多个情节提要。我想在某些控制器的导航栏下方的顶部始终添加一个视图。我该如何实现?
我已经使用导航委托并在窗口中添加视图,但是没有运气。在所附图像中显示灰色视图的步骤是。 1.单击该视图控制器上的按钮;灰色的视图应该显示并保留在控制器的顶部,直到对用户是否应该使用任何viewController的设备进行所有扫描为止。
答案 0 :(得分:1)
您可以创建一个UINavigationController子类,然后在其中添加视图。
class NavigationController: UINavigationController {
let customView = UIView()
let iconImgView = UIImageView()
let msgLbl = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
customView.isHidden = true
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .gray
view.addSubview(customView)
iconImgView.contentMode = .scaleAspectFit
iconImgView.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(iconImgView)
msgLbl.numberOfLines = 0
msgLbl.lineBreakMode = .byWordWrapping
msgLbl.textColor = .white
msgLbl.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(msgLbl)
customView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
customView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
customView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
iconImgView.widthAnchor.constraint(equalToConstant: 40).isActive = true
iconImgView.heightAnchor.constraint(equalToConstant: 40).isActive = true
iconImgView.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
iconImgView.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 15).isActive = true
iconImgView.trailingAnchor.constraint(equalTo: msgLbl.leadingAnchor, constant: 15).isActive = true
msgLbl.topAnchor.constraint(equalTo: customView.topAnchor, constant: 10).isActive = true
msgLbl.bottomAnchor.constraint(equalTo: customView.bottomAnchor, constant: 10).isActive = true
msgLbl.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -15).isActive = true
msgLbl.heightAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true
}
func showCustomView(message: String, icon: UIImage) {
msgLbl.text = message
iconImgView.image = icon
customView.isHidden = false
}
func hideCustomView() {
customView.isHidden = true
}
}
将所有视图控制器嵌入此导航控制器中。如果要在视图控制器中显示/隐藏灰色视图,请使用
显示
(self.navigationController as? NavigationController)?.showCustomView(message: "Any Message", icon: UIImage(named: "anyImage")!)
隐藏
(self.navigationController as? NavigationController)?.hideCustomView()
当您从同一导航控制器中推入另一个视图控制器时,在您调用hide方法之前,视图不会被隐藏
答案 1 :(得分:0)
您可以简单地使用相关的UIView
创建自定义frame
并在要添加到其中的addSubview()
上调用view
。
lazy var customView: UIView = {
let customView = UIView(frame: CGRect.init(x: 0, y: self.view.safeAreaInsets.top, width: UIScreen.main.bounds.width, height: 100))
customView.backgroundColor = .gray
return customView
}()
@IBAction func onTapButton(_ sender: UIButton) {
self.view.addSubview(customView)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.customView.removeFromSuperview()
}
要将其添加到navigationBar
下,请将y
的{{1}}位置用作frame
。这样,您的self.view.safeAreaInsets.top
将始终在customView
下方对齐。
您可以根据需要使用navigationBar
创建视图。我用过height
。
给出正确的height = 100
,您可以将任何视图作为frame
添加到另一个视图。