gpt4 book ai didi

iOS 10 自定义导航栏高度

转载 作者:IT王子 更新时间:2023-10-29 05:45:58 26 4
gpt4 key购买 nike

我通过使用以下代码对其进行子类化来实现自定义导航栏高度

class TMNavigationBar: UINavigationBar {

///The height you want your navigation bar to be of
static let navigationBarHeight: CGFloat = 44.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 = TMNavigationBar.heightIncrease/2

///Transform all view to shift upward for [shift] point
self.transform =
CGAffineTransformMakeTranslation(0, -shift)
}

override func layoutSubviews() {
super.layoutSubviews()

let shift = TMNavigationBar.heightIncrease/2

///Move the background down for [shift] point
let classNamesToReposition: [String] = ["_UINavigationBarBackground"]
for view: UIView in self.subviews {
if classNamesToReposition.contains(NSStringFromClass(view.dynamicType)) {
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 = CGSizeMake(amendedSize.width, TMNavigationBar.navigationBarHeight);
return newSize;
}
}

以下问题仅在 iOS 10 上出现:(栏和 View 之间的黑色空间)

enter image description here

不知道那里发生了什么。但是在 Storyboard 中它生成了这个警告,并且没有办法在 IB 中修复它(警告仅在我更改 IB 中导航栏的子类时出现)。

enter image description here

最佳答案

适用于 iOS 10、Swift 3.0:

extension UINavigationBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
let screenRect = UIScreen.main.bounds
return CGSize(width: screenRect.size.width, height: 64)
}
}

关于iOS 10 自定义导航栏高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454548/

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