iOS 10 custom navigation bar height

天大地大妈咪最大 提交于 2019-11-29 10:11:33

Works on 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)
    }
}

I checked Interface debugger and this is what i see (so basically it's trying to change navigation bar height, bit it's stays same and it's showing just black space - which is window color):

With later investigation i noticed that it's not calling: "_UINavigationBarBackground"

Then i checked view.classForCoder from fast enumeration, and discovered that key is changed to "_UIBarBackground", so i updated layoutSubviews():

override func layoutSubviews() {
    super.layoutSubviews()

    let shift = TMNavigationBar.heightIncrease/2

    ///Move the background down for [shift] point
    let classNamesToReposition = isIOS10 ? ["_UIBarBackground"] : ["_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
        }
    }
}

Cheers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!