简体   繁体   中英

center navigation bar views vertically

I managed to increase the height of my navigation bar, but I faced with the problem, that title and buttons are situated in the bottom of navigator bar.

extension UINavigationBar {

    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        let v = self.value(forKey: "frame") as? CGRect
        return v?.size ?? CGSize(width: UIScreen.main.bounds.width, height: 44)
    }

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 64)
}

在此处输入图片说明

I tried to set offset, using

navigationBar.setTitleVerticalPositionAdjustment(-10, for: .default)
navigationItem.leftBarButtonItem?.setBackgroundVerticalPositionAdjustment(-10, for: .default)
navigationItem.rightBarButtonItem?.setBackgroundVerticalPositionAdjustment(-10, for: .default)
navigationItem.rightBarButtonItem?.setBackButtonBackgroundVerticalPositionAdjustment(-10, for: .default)
navigationItem.leftBarButtonItem?.setBackButtonBackgroundVerticalPositionAdjustment(-10, for: .default)

Actually that setting only apply to my title, and the position of button bars remain the same.

But I don't think my solution is the best.

Its not a good practice to increase the size of the navigation bar. Instead you can add a uiview right under navigation bar with same color and remove the navigation bar border to make to look like height increased. Here is an example how yelp does it. 在此处输入图片说明

I posted the solution of this problem in another thread. Hope this helps.

vertically aligning UINavigationItems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM