Change UINavigationBar Height

前端 未结 12 1484
囚心锁ツ
囚心锁ツ 2020-11-28 09:26

Can some one tell me how to change the navigation bar height?

Here is what i have so far:

CGFloat navBarHeight = 10;
self.navigationController.navi         


        
相关标签:
12条回答
  • 2020-11-28 09:58

    Try using -

    CGFloat navBarHeight = 10.0f;    
    CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
    [self.navigationController.navigationBar setFrame:frame];
    
    0 讨论(0)
  • 2020-11-28 09:58

    self.navigationController.navigationBar.frame is a readonly property on iOS version 4.x

    it will be changed even though, you will see abnormal behavior.

    for example, when app state changed to background and get back to foreground the Navigation bar will show original sized height

    0 讨论(0)
  • 2020-11-28 09:58

    Creating category and overriding sizeThatFits(_ size: CGSize) -> CGSize method changes UINavigationBar size all over the app. Assuming you want to change UINavigationBar height/size only in one particular ViewController (in my case)

    This is how I did it.

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

    in caller ViewController would look like

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationBar()
    }
    
    private func configureNavigationBar() {
        var naviBarFrame = self.navigationController?.navigationBar.frame
        naviBarFrame?.size.height = 74
        let rect = naviBarFrame ?? CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 74)
        self.navigationController?.navigationBar.setValue(rect, forKey: "frame")
    }
    
    0 讨论(0)
  • 2020-11-28 10:03

    I ran into a lot of problems depending on the method chosen. In my specific case, the navigation bar was hidden throughout the app except in the Settings View Controller. As I needed to specify a custom height of the navigation bar and show the navigation bar at the same time, the segue between the source view controller and the Settings View Controller had a very sloppy animation.

    The absolute best solution can be found here: https://gist.github.com/maciekish/c2c903d9b7e7b583b4b2. I just imported the category into the Settings View Controller, and in viewDidLoad called the setHeight: method on `self.navigationController.navigationBar'. Looks great!

    0 讨论(0)
  • 2020-11-28 10:06

    sizeThatFits: no longer works with iOS11 UINavigationBar subclasses. Apple Engineering have confirmed that this technique was never supported and with iOS11 changes sizeThatFits: is no longer consulted.

    0 讨论(0)
  • 2020-11-28 10:07

    I have solved this issue, I created the subclass of UINavigationController. In this MainNavigationViewController.m file I rewrite viewDidLayoutSubviews() method, where I set the frame for self.navigationBar

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