Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change height of UINavigationBar
import Foundation
private var AssociatedObjectHandle: UInt8 = 0
extension UINavigationBar {
var height: CGFloat {
get {
if let h = objc_getAssociatedObject(self, &AssociatedObjectHandle) as? CGFloat {
return h
}
return 0
}
set {
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
override public func sizeThatFits(size: CGSize) -> CGSize {
if self.height > 0 {
return CGSizeMake(self.superview!.bounds.size.width, self.height);
}
return super.sizeThatFits(size)
}
}
@siberianisaev

This comment has been minimized.

Copy link
Owner Author

commented Dec 11, 2014

@samuelbeek

This comment has been minimized.

Copy link

commented Jun 3, 2015

Thank you so much, really helped me out!

@degt

This comment has been minimized.

Copy link

commented Oct 22, 2015

Thanks!

To make it works with Xcode7 + swift 2.0 replace line 16 for this one:

objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
@YHSX88

This comment has been minimized.

Copy link

commented Apr 8, 2016

Thanks siberianisaev and Degt, I got error when using 7.3 with 2.2 but all is good now thanks

@lgeefs

This comment has been minimized.

Copy link

commented Jul 13, 2016

Thank you

@avaDeveloper

This comment has been minimized.

Copy link

commented Oct 20, 2016

where should call sizeThatFits method ?

@jasonwiener

This comment has been minimized.

Copy link

commented Oct 22, 2016

thanks a ton for this!

@jasonwiener

This comment has been minimized.

Copy link

commented Oct 25, 2016

@avaDeveloper, it's called internally by the navBar, you don't explicitly call it.

@felixdkatt

This comment has been minimized.

Copy link

commented Jun 29, 2017

when i use this my title view and buttons are off center closer to the bottom

@n8chur

This comment has been minimized.

Copy link

commented Jul 20, 2017

@felixdkatt you should be able to transform the view within sizeThatFits() to resolve this in iOS 10 and below:

self.transform = CGAffineTransformMakeTranslation(0, -(heightDelta));

But this hack appears to be broken in the iOS 11 beta and it doesn't look likely that it will be fixed according to this thread.

@hemangshah

This comment has been minimized.

Copy link

commented Aug 24, 2017

Please update it for the latest Swift.

@hilalbaig

This comment has been minimized.

Copy link

commented Sep 25, 2017

Thanks, here is its swift 3 version

@annjawn

This comment has been minimized.

Copy link

commented Dec 14, 2017

What is the usage for this? Where should I put the height?

@AlexanderBollbach

This comment has been minimized.

Copy link

commented Oct 17, 2018

what is the usage for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.