Ios 如何将视图添加为某些控制器的子视图
我的应用程序中有多个故事板。我想为一些控制器在导航栏的正下方添加一个始终位于顶部的视图。我怎样才能做到这一点 我已经使用了导航代理并在窗口中添加了一个视图,但运气不好。在附加图像中显示灰色视图的步骤如下所示。 1.单击该视图控制器上的按钮;灰色视图应显示并保持在控制器顶部,直到未完成设备的所有扫描,无论用户是否应使用任何ViewControllerIos 如何将视图添加为某些控制器的子视图,ios,swift,uiviewcontroller,uinavigationcontroller,uiwindow,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,Uiwindow,我的应用程序中有多个故事板。我想为一些控制器在导航栏的正下方添加一个始终位于顶部的视图。我怎样才能做到这一点 我已经使用了导航代理并在窗口中添加了一个视图,但运气不好。在附加图像中显示灰色视图的步骤如下所示。 1.单击该视图控制器上的按钮;灰色视图应显示并保持在控制器顶部,直到未完成设备的所有扫描,无论用户是否应使用任何ViewController 您只需使用相关的框架创建一个自定义ui视图,并在视图上调用addSubview(),即可将其添加到其中 lazy var customView: U
您只需使用相关的
框架创建一个自定义ui视图
,并在视图上调用addSubview()
,即可将其添加到其中
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()
}
要将其添加到导航栏下方
,请使用y
帧的位置作为self.view.safeAreaInsets.top
。这样,您的customView
将始终在导航栏下方对齐
您可以根据需要创建高度为的视图。我使用了height=100
给出正确的框架
,您可以将任何视图作为子视图添加到另一个视图中
您可以创建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()
当您从同一导航控制器推送另一个视图控制器时,视图将不会隐藏,直到您调用隐藏方法是否希望该视图始终位于导航控制器的导航栏下?@Rico Crescenzio-是的,我希望该视图始终位于导航控制器的导航栏下,但对于某些控制器也是如此是否尝试将视图添加到navigationController视图?类似于navigationController?.view.addSubview(grayView)
它将在导航视图上添加视图,而不是在导航视图下方。是的,您必须调整视图位置,例如,向导航添加约束bar@PGDev-这将为所有控制器添加视图,但我想为一些控制器添加此视图,然后对特定控制器使用self.view.addSubview(customView)
。此外,如果没有帮助,请进一步澄清要求。