【问题标题】:How to add a view as subview for certain controllers如何将视图添加为某些控制器的子视图
【发布时间】:2019-05-21 10:40:07
【问题描述】:

我的应用中有多个故事板。我想在某些控制器的导航栏下方的顶部始终添加一个视图。我怎样才能做到这一点?

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

【问题讨论】:

  • 您希望该视图始终位于导航控制器的导航栏下方吗?
  • @ Rico Crescenzio - 是的,我希望该视图始终位于导航控制器的导航栏下,但对于某些控制器
  • 那么您是否尝试将视图添加到 navigationController 视图中?喜欢navigationController?.view.addSubview(grayView)
  • 它将在导航视图上添加视图而不是在导航视图下方。
  • 是的,您必须调整视图位置,例如向导航栏添加约束

标签: ios swift uiviewcontroller uinavigationcontroller uiwindow


【解决方案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 方法

【讨论】:

    【解决方案2】:

    您可以简单地使用相关的frame 创建一个自定义UIView,然后在要添加它的view 上调用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()
    }
    

    要将其添加到navigationBar 下方,请将framey 位置用作self.view.safeAreaInsets.top。这样您的customView 将始终在navigationBar 下方对齐。

    您可以根据您的要求使用height 创建视图。我用过height = 100

    提供正确的frame,您可以将任何视图作为subView 添加到另一个视图。

    【讨论】:

    • @PGDev- 这将为所有控制器添加视图,但我想为某些控制器添加此视图
    • 然后对特定的控制器使用self.view.addSubview(customView)
    • 另外,请再澄清一下要求,以防它没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多