Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change height of UINavigationBar - Swift 3
import Foundation
import UIKit
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 open func sizeThatFits(_ size: CGSize) -> CGSize {
if self.height > 0 {
return CGSize(width: UIScreen.main.bounds.width, height: self.height)
}
return super.sizeThatFits(size)
}
}
@hilalbaig

This comment has been minimized.

Copy link
Owner Author

commented Sep 25, 2017

Swift 3 version of UINavigationBar+Height.swift

@daurenm

This comment has been minimized.

Copy link

commented Oct 18, 2017

Where should I put "navigationBar.height = someValue"?

@genaks

This comment has been minimized.

Copy link

commented Nov 25, 2017

Anybody else get this working? I am trying to change the height using -
nav.navigationController?.navigationBar.height = 100.0
but nothing has happened so far.

@Yuvraj-Sorav

This comment has been minimized.

Copy link

commented Dec 28, 2017

I have tried all the version of swift as well as objective-C.It worked well till iOS 10 but it is not working in iOS11. When i am going to set the height of navigation bar to 130, it will return the height 96. I don't know why it is so?Any help would be appreciated.

@prashantdabhi9033

This comment has been minimized.

Copy link

commented Apr 3, 2019

in iOS 11 it won't allow changing the frame of the navigation bar.

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.