我正在尝试在 UINavigationController
中的 UINavigationBar
下方添加一个 UIView
。
该 View 将用作信息消息的占位符(例如,如果我们遇到问题并且内容未更新)。
添加 View 本身并设置其约束不是问题,但它与导航 Controller 中包含的 View 内容重叠,这不是我想要的。如何设置所包含 View Controller 的内容以尊重此新 View 占用的空间?
屏幕截图显示了我的自定义(橙色) View 与推送到导航 Controller 的 viewController 的内容重叠。
最佳答案
尝试对 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/