gpt4 book ai didi

ios - 如何在 Swift 中增加自定义 UINavigationBar 类的高度

转载 作者:搜寻专家 更新时间:2023-11-01 06:02:21 39 4
gpt4 key购买 nike

更新:我最终隐藏了默认导航栏并添加了一个看起来与导航栏相同的 UIView。这听起来可能不太好,但不是修补到 UINavigationBar 这很好。


这是我创建的自定义 UINavigationBar 类,用于增加应用中导航栏的高度。它对我不起作用。这是 code .

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. 

Modifying the Navigation Bar Object Directly

您可以使用自定义 View 作为导航栏。根据您的要求自定义 View (例如更改高度)并将默认导航栏隐藏为

self.navigationController?.setNavigationBarHidden(true, animated: false)

关于ios - 如何在 Swift 中增加自定义 UINavigationBar 类的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45853469/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com