简体   繁体   English

在导航控制器的导航栏下添加视图

[英]Adding view beneath navigation bar in navigation controller

I'm trying to add a UIView beneath the UINavigationBar in my UINavigationController . 我正在尝试在UINavigationBar中的UINavigationController下面添加一个UIView The view will serve as a placeholder for information messages (for example if we are having issues and content is not getting updated). 该视图将用作信息消息的占位符(例如,如果我们遇到问题且内容未得到更新)。

Adding the view it self and setting it's constraints is not an issue, but it is overlapping the content of the views that is contained in the navigation controller, which is not want I want. 添加自己的视图并设置它的约束不是问题,但它与导航控制器中包含的视图内容重叠,这是我不想要的。 How can I set the content of the contained viewcontroller to respect the space which this new view takes up? 如何设置包含的viewcontroller的内容以尊重此新视图占用的空间?

The screenshot is showing my custom (orange) view overlapping the content of the viewController that was pushed on to the navigation controller. 屏幕截图显示了我的自定义(橙色)视图,该视图与推送到导航控制器的viewController的内容重叠。

在此输入图像描述

Try Subclassing the UINavigationController and then add your orange view's height constraint to it. 尝试对UINavigationController进行子类化,然后将橙色视图的高度约束添加到其中。 and call the function whenever you need it 并在需要时调用该函数

import UIKit
class CustomNavigationController: UINavigationController{

    @IBOutlet weak var topViewHeight: NSLayoutConstraint!

    func animateHeight(height: CGFloat){
        UIView.animate(withDuration: 0.2) {
            self.viewControllers.forEach{ vc in
                let v = vc.view.frame
                vc.view?.frame = CGRect(x: 0, y: height, width: v.width, height: v.height)
            }
            self.topViewHeight.constant = height
        }
    }
}

how to use it? 如何使用它? in your VC where you want to show/hide it: 在你想要显示/隐藏它的VC中:

(self.navigationController as? CustomNavigationController)?.animateHeight(height: 50)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM