ios - 在导航 Controller 中的导航栏下方添加 View

标签 ios swift autolayout uinavigationbar

我正在尝试在 UINavigationController 中的 UINavigationBar 下方添加一个 UIView。 该 View 将用作信息消息的占位符(例如,如果我们遇到问题并且内容未更新)。

添加 View 本身并设置其约束不是问题,但它与导航 Controller 中包含的 View 内容重叠,这不是我想要的。如何设置所包含 View Controller 的内容以尊重此新 View 占用的空间?

屏幕截图显示了我的自定义(橙色) View 与推送到导航 Controller 的 viewController 的内容重叠。

enter image description here

最佳答案

尝试对 UINavigationController 进行子类化,然后向其添加橙色 View 的高度约束。并在需要时调用该函数

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
        }
    }
}

如何使用它? 在你想要显示/隐藏它的 VC 中:

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

关于ios - 在导航 Controller 中的导航栏下方添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56734687/

相关文章:

swift - 通过更改约束常量来动画 View

ios - 通过蓝牙/wifi在2个设备之间进行音频流传输?

iphone - 如何更改导航栏上 UIBarButtonItem 的字体颜色/文本颜色

swift - 如何在 SwiftUI 中分配可选的绑定(bind)参数?

IOS - 生产上的 ScheduleLocalNotification

ios - NSLayoutAttribute 的解释

swift - Snapkit:将倍数限制为边距

ios - 在 Xcode 中构建阶段后运行 Applescript

iphone - 比较相等的 NSDate 将返回它们不相等

swift - XCode 8 Beta 6 中引入的 UISlider 错误的解决方法?