iOS 10 custom navigation bar height

后端 未结 2 1593
遥遥无期
遥遥无期 2020-12-19 04:15

I implemented custom navigation bar height, by subclassing it with following code

class TMNavigationBar: UINavigationBar {

    ///The height you want your n         


        
相关标签:
2条回答
  • 2020-12-19 04:24

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-19 04:42

    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.

    0 讨论(0)
提交回复
热议问题