iOS 10自定义导航栏高度

iOS 10自定义导航栏高度,第1张

概述我实现了自定义导航栏高度,通过以下代码继承它 class TMNavigationBar: UINavigationBar { ///The height you want your navigation bar to be of static let navigationBarHeight: CGFloat = 44.0 ///The difference betwe 我实现了自定义导航栏高度,通过以下代码继承它
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上出现以下问题:(条形图和视图之间的黑色空格)

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

解决方法 适用于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自定义导航栏高度全部内容,希望文章能够帮你解决iOS 10自定义导航栏高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1109006.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存