After applying the code in the accepted answer, the height does not change at all.
This is not an easy job ... and I have looked at several articles on the Internet (most of them in Objective-C).
The most useful of them: http://www.emdentec.com/blog/2014/2/25/hacking-uinavigationbar
But his final decision does not put elements in the middle, not in Swift.
So, I came up with a workable version in Swift . Hope this helps some people since I was saved so much precious time on SO.
Solution in Swift:
The following code will help solve some of the problems you are having:
- The title and elements do not fit in the middle of the navigation bar
- The title and elements will be displayed when the user moves between view managers.
First you need to subclass UINavigationBar , and in your storyboard, select the navigation bar item, and on the Identity Inspector tab , set the new class as a Custom class
import UIKit class UINavigationBarTaller: UINavigationBar { ///The height you want your navigation bar to be of static let navigationBarHeight: CGFloat = 64 ///The difference between new height and default height static let heightIncrease:CGFloat = navigationBarHeight - 44 override init(frame: CGRect) { super.init(frame: frame) initialize() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initialize() } private func initialize() { let shift = UINavigationBarTaller.heightIncrease/2 ///Transform all view to shift upward for [shift] point self.transform = CGAffineTransformMakeTranslation(0, -shift) } override func layoutSubviews() { super.layoutSubviews() let shift = UINavigationBarTaller.heightIncrease/2 ///Move the background down for [shift] point let classNamesToReposition: [String] = ["_UINavigationBarBackground"] for view: UIView in self.subviews { if classNamesToReposition.contains(NSStringFromClass(view.dynamicType)) { let bounds: CGRect = self.bounds var frame: CGRect = view.frame frame.origin.y = bounds.origin.y + shift - 20.0 frame.size.height = bounds.size.height + 20.0 view.frame = frame } } } override func sizeThatFits(size: CGSize) -> CGSize { let amendedSize:CGSize = super.sizeThatFits(size) let newSize:CGSize = CGSizeMake(amendedSize.width, UINavigationBarTaller.navigationBarHeight); return newSize; } }
Also in my text: https://gist.github.com/pai911/8fa123d4068b61ad0ff7
IOS 10 update:
Unfortunately, this code is broken on iOS 10, there is someone who helps to fix it, here you are:
height of customizable iOS 10 navigation bar
And to be clear, this code is a hack, because it depends on the internal structure of the navigation bar ... therefore, if you decide to use it in any case, be prepared for any upcoming changes that may violate this code ...
John the Traveler
source share