Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilalbaig/5a4dd45617a4eb985d3cef986fb7abb6 to your computer and use it in GitHub Desktop.
Save hilalbaig/5a4dd45617a4eb985d3cef986fb7abb6 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Swift 3 version of UINavigationBar+Height.swift

@daurenm
Copy link

daurenm commented Oct 18, 2017

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

@genaks
Copy link

genaks 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
Copy link

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
Copy link

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