更新:我最终隐藏了默认导航栏并添加了一个与导航栏相同的 UIView。这听起来可能不太好,但与其修补,不如说UINavigationBar
这很好。
这是我UINavigationBar
创建的自定义类,用于增加应用程序中导航栏的高度。它对我不起作用。这是代码。
class PPBaseNavigationBar: UINavigationBar {
///The height you want your navigation bar to be of
static let navigationBarHeight: CGFloat = 83.0
///The difference between new height and default height
static let heightIncrease: CGFloat = navigationBarHeight - 44
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
let shift = PPBaseNavigationBar.heightIncrease/2.0
///Transform all view to shift upward for [shift] point
self.transform = CGAffineTransform(translationX: 0, y: -shift)
}
override func layoutSubviews() {
super.layoutSubviews()
let shift = PPBaseNavigationBar.heightIncrease/2.0
///Move the background down for [shift] point
var classNamesToReposition: Array<String>?
if #available(iOS 10.0, *) {
classNamesToReposition = ["_UIBarBackground"]
} else {
classNamesToReposition = ["_UINavigationBarBackground"]
}
for view: UIView in self.subviews {
if (classNamesToReposition?.contains(NSStringFromClass(view.classForCoder)))! {
let bounds: CGRect = self.bounds
var frame: CGRect = view.frame
frame.origin.y = bounds.origin.y + shift - 20.0
frame.size.height = bounds.size.height + 20.0
view.frame = frame
}
}
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let amendedSize:CGSize = super.sizeThatFits(size)
let newSize:CGSize = CGSize.init(width: amendedSize.width, height: PPBaseNavigationBar.navigationBarHeight)
return newSize;
}
}
除了override func sizeThatFits(_ size: CGSize) -> CGSize {...}
不确定之外,所有方法都被调用,为什么?
It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly.
您可以使用自定义视图作为导航栏。根据您的要求自定义视图(例如更改高度)并将默认导航栏隐藏为
self.navigationController?.setNavigationBarHidden(true, animated: false)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句