如何为某些控制器添加视图作为子视图

时间:2019-05-21 10:40:08

标签: ios swift uiviewcontroller uinavigationcontroller uiwindow

我的应用中有多个情节提要。我想在某些控制器的导航栏下方的顶部始终添加一个视图。我该如何实现?

我已经使用导航委托并在窗口中添加视图,但是没有运气。在所附图像中显示灰色视图的步骤是。 1.单击该视图控制器上的按钮;灰色的视图应该显示并保留在控制器的顶部,直到对用户是否应该使用任何viewController的设备进行所有扫描为止。

enter image description here

2 个答案:

答案 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添加到另一个视图。

enter image description here