Is there a way to change the height of a UINavigationBar in Storyboard without using a UINavigationController?
You can set the property barPosition
to UIBarPositionTopAttached
another way!
- Add a delegate to your UINavigationBar.
Implement
-positionForBar:
in the delegate class:- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Your navigation bar's top must also be anchored to the Top Layout Guide.
different navigationbar Height in storyboard
do like select your Viewcontroller
--> select navigationBar
--> go to attribute Inspector on NavigationItem
--> select Prompt
--> add one/More space in your keyboard on their field , it increase size on your TopBar/NavigationBar Height
for example
How can I change height of Navigation Bar - Swift 3
UPDATE 8/3/20: I posted this in 2016. A number of people have stated this no longer works so please use at your own risk. I am not working in iOS at the moment so I do not have an update handy. Best of luck!
Here is one way to do it:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
I want to change the height of UINavigationBar and add a title..Why is this so difficult?
In a navigation interface, a navigation controller owns its
UINavigationBar object and is responsible for managing it. It is not
permissible to change the navigation bar object or modify its bounds,
frame, or alpha values directly. However, there are a few properties
that you can modify
More here: apple documentation on customizing the nav bar
You can use the UINavigationItem
navigationItem property in UIViewController
to customize your UINavigationBar
.
// In ViewController
self.navigationItem.titleView = myTitleLabel;
self.navigationItem.rightBarButtonItem = myIcon;
self.navigationItem.leftBarButtonItem = myBackIcon;
Programmatically change height of navigation bar in swift
update for iOS 11 and beyond
apple doesn't want you messing with the navigation bar height, so don't touch it
see here: https://openradar.appspot.com/32912789
and here: https://forums.developer.apple.com/thread/88202#274620
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(self.superview!.frame.size.width, 87)
return newSize
}
}
I don't do swift, but the answer is easy, here's ObjC
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake([self superview].frame.size.width, 40);
}
Here's my interpretation in Swift:
import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(superview.width, 87)
return newSize
}
}
The problem you will have isn't with this method, this is the easy part, the problem is forcing the navigation controller to always use this navigation bar
I subclass and resize everything in IOS, including navigation controllers and tabbarcontrollers, in order to enforce that all navigation controllers use this navigation bar, you must subclass a navigationController and only use this navigationcontroller throughout your app, here's how the subclass works, this is Obj C, so you'll have to translate it:
@interface NSHNavigationController () <UINavigationBarDelegate, UINavigationControllerDelegate>
{
}
@implementation NSHNavigationController
#pragma mark Initialization
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
{
self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:toolbarClass];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (void)dealloc
{
[self setInternalDelegate:nil];
[self setExternalDelegate:nil];
}
#pragma mark Setup
- (void)NSHSetupNavigationController
{
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
}
this is the line that will do it for you:
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
Oh yeah, and make sure you are subclassing the nav bar, you said you were, but here's how you do it, it's simple:
#import "NSHNavigationBar.h"
@implementation NSHNavigationBar
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:fontMagicForRegularNSHFont};
[self setTitleTextAttributes:attributes];
[self setTranslucent:true];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake([self superview].frame.size.width, heightResizer(40));
}
- (void)layoutSubviews {
[super layoutSubviews];
}
So, in summary, subclass both the UINavigationBar and the UINavigationController and you are set, this will allow you to manipulate the navigation bar whenever you'd like, you can also type cast your view controller's navigation bars, this is a little nuts and will confuse a lot of people, but here it goes:
-(CustomNavigationBar *)navBar {
return (id)[self.navigationController navigationBar];
}
put the stuff above in your view controller and then you call it like this:
[[self navBar] setBackGroundColor:[UIColor blueColor]];
This will successfully typecast your navigationcontroller to be your custom navigation bar, if you want to change the height of the nav bar from here, then you can do something like this:
Related Topics
When Exactly Do Implicit Animations Take Place in iOS
Change Uiimageview Size to Match Image with Autolayout
How to Play Video Stream with Mpmovieplayercontroller in iOS
Why Can't I Call the Default Super.Init() on Uiviewcontroller in Swift
Mfmailcomposeviewcontroller in iOS 7 Statusbar Are Black
Xcode Cannot Run on the Selected Destination
@Property/@Synthesize Equivalent in Swift
Found an Unexpected MACh-O Header Code: 1918975009 in Xcode 6
How to Calculate the Uilabel Height Dynamically
Where Do I Find iOS Obj-C Code to Scan and Connect to Wifi (Private API)
Apple MACh-O Linker Warning "Directory Not Found for Option..."
Uiprogressview Custom Track and Progress Images in iOS 7.1
Uitableview Reloaddata Automatically Calls Resignfirstresponder