iOS 10 custom navigation bar height

2020-02-12 05:26发布

问题:

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

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;
    }
}

Following problem occurs only on iOS 10: (black space between bar & view)

No idea what's happening there. But in storyboard it's generated this warning, and there's no way to fix it in IB (warning only appears when i change subclass of navigation bar in IB).

回答1:

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


回答2:

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.