简体   繁体   中英

How to increase the height of navigation bar in Xcode?

I'm working on a app, in which I need to keep a navigation bar. when I write any title on the bar, the time and the title kinda get very close to each other. I wanted to increase the height of the bar, so it can get some breathing room.

select your ViewController --> select your Navigation Item --> Prompt --> Add space it increase the height of **Navigation bar**

Check Image here : 在此处输入图片说明

Programatically

Add this in viewWillAppear or viewDidAppear method

Objective-C

[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.view.frame.size.width,80.0)];

Swift

self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 80.0)

Swift-3

self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80.0)

iOS 11

在此处输入图片说明

objective C

for (UIView *subview in self.navigationController.navigationBar.subviews) {
    if ([NSStringFromClass([subview class]) containsString:@"BarBackground"]) {
        CGRect subViewFrame = subview.frame;
        // subViewFrame.origin.y = -20;
        subViewFrame.size.height = 100;
        [subview setFrame: subViewFrame];
    }
}

swift

for subview in (self.navigationController?.navigationBar.subviews)! {
       if NSStringFromClass(subview.classForCoder).contains("BarBackground") {
            var subViewFrame: CGRect = subview.frame
            // subViewFrame.origin.y = -20;
            subViewFrame.size.height = 100
            subview.frame = subViewFrame

        }

    }

Apple proposes not to resize navigationBar itself, but remove shadow from bar and add custom view under your navigationBar . This can work for most cases. Check Apple's samples.

THIS SOLUTION NO LONGER WORKS IN Xcode 8.xx and later!

you can also increase height without creating the custom navigation follow the following steps

Step 1 Selecte Navigation bar in Storyboard or XIB

在此处输入图片说明

Step 2 Copy ObjectID from Identity Inspector

在此处输入图片说明

Step 3 Open Storyboard/XIB as Source Code

在此处输入图片说明

Step 4 Find ObjectID in Source Code past ObjectID in search

在此处输入图片说明

Step 5 Edit height! thats all

在此处输入图片说明

I hope this will help you

Please refer the apple recommended approach for extended navigation bar here,

https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html

Add the following extension to your project:

import UIKit

extension UINavigationBar {

    override open func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: 80.0)
    }

}

Add this in viewWillAppear method

CGFloat height = 80;
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0,
self.view.frame.size.width,height)];

if it increase first and shrinks to original height then add this code in viewDidAppear method

simply add this line to your viewController

navigationController?.additionalSafeAreaInsets.top = 30 
  // where 30 is the extra space, add as per your need. 

We need to change the height of the navigation bar for each time the view show.So put the code on viewWillAppear

override func viewWillAppear(_ animated: Bool) {
     self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 80)
}

we can set the width as the width of the view and change the height as we wish.

You can't change the height of the default NavigationBar if I'm not wrong.

Although, you can create a custom NavigationBar and add a custom height to it.

I would hide the navigation bar and implement a custom one on my own. That way you could style it exactly as you want.

There is a simple tutorial on how to do that: https://www.codeschool.com/blog/2014/11/11/ios-app-creating-custom-nav-bar/

    navigationController?.additionalSafeAreaInsets.top = 25

Add this to viewDidLoad. it will definitely work. Successfully worked in Xcode 12-version

If you're

  • using a storyboard (which you said you are)
  • using autolayout, and
  • using a manually-added Navigation Bar (as opposed to one provided by a Navigation Controller)

then you can simply set a height constraint on the Navigation Bar.

xcode添加新的约束对话框

[self.navigationController.navigationBar setPrefersLargeTitles:YES]; is going to Increase the Navigation bar height Programmatically

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