IOS 8 Swift Xcode 6 - set top navigation bar bg color and height

I looked everywhere and tested all the code snippets hosted on Stack, but nothing works for me, because I need it to work.

I just want to install:

  • Navigation Bar Height
  • Bg navigation bar color in RGB
  • Logo with Nav bar logo

I work with iOS8, Xcode 6 and Swift.

Thanks so much for the clear answer!

This is my code in ViewController.swift

// Set nav bar height navigationController?.navigationBar.frame.origin.y = -10 // Set nav bar bg color var navBarColor = UIColor(red: 4 / 255, green: 47 / 255, blue: 66 / 255, alpha: 1) navigationController?.navigationBar.barTintColor = navBarColor // Set nav bar logo let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) navBarImageView.contentMode = .ScaleAspectFit let navBarImage = UIImage(named: "navBarLogo.png") navBarImageView.image = navBarImage navigationItem.titleView = navBarImageView 
+7
xcode ios8 swift navbar
source share
3 answers

Navigation Bar Height:

In a custom subclass of the navigation controller ...

The trick with this is to NOT change the actual height of the navigation bar and instead adjust its start.

 func viewDidLoad() { super.viewDidLoad() navigationBar.frame.origin.y = -10 } 

Bg navigation bar color in RGB:

In a custom subclass of the navigation controller ...

 func viewDidLoad() { super.viewDidLoad() navigationBar.barTintColor = // YOUR COLOR } 

or use appearance proxy

 UINavigationBar.appearance().barTintColor = // YOUR COLOR 

Logo with Nav bar logo

In the user view controller ...

 func viewDidLoad() { super.viewDidLoad() navigationItem.titleView = UIImageView(image: // YOUR LOGO) } 
+4
source share

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 ...

+12
source share

Great answer from Bon Bon !

In Swift 3, however, make sure you replace

 let classNamesToReposition: [String] = ["_UINavigationBarBackground"] 

from

 let classNamesToReposition: [ String ] = [ "_UIBarBackground" ] 

Otherwise it will not work.

+2
source share

All Articles