2

I have a UINavigationController image that I'm setting in the App Delegate's didFinishLaunchingWithOptions like this:

var image: UIImage = UIImage(named: "Header")!
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: .Default)

The Header is the file in the Assets.xcassets with the 2x and 3x size images. I took out the 1x size because I read it was necessary and it wasn't making a difference either way. On an iPhone 6 the image is perfectly centered. While on a iPhone5 it's more to the right. When I put the above code in the viewDidLoad method in my file, I get a blank header.

I also tried resizing the image:

 var image: UIImage = UIImage(named: "Header")!
        image.resizableImageWithCapInsets(UIEdgeInsetsMake(0, -260, 0, 0))
        UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: .Default)

I even plugged in random numbers to see if anything changes and it doesn't.

Anyone have any idea what to do?

I referenced these links:

  1. UINavigationBar custom title position

  2. Misaligned title in UINavigationBar since iOS6

  3. Center UIImage in UINavigationController

UPDATE

Centering the UIImage on the NavigationBar still doesn't work on the iPhone5.

Community
  • 1
  • 1
Lukesivi
  • 2,206
  • 4
  • 25
  • 43

1 Answers1

2

Below is the code snip i have added in my app delegate. which works perfect for me.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        //Cutomize navigation bar
        let navBgImage:UIImage = UIImage(named: "dummy_navigation_bg_image")!
        UINavigationBar.appearance().setBackgroundImage(navBgImage, forBarMetrics: .Default)
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()];

        return true
    }
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • Hey, thanks for the reply. It didn't move. Could there be a reason it not moving? Could it be a problem with the images? – Lukesivi Dec 01 '15 at 10:47
  • just make sure you have proper image size, it should be not smaller (lets try 630x70 for 1x) – swiftBoy Dec 01 '15 at 10:52
  • Ok yes, I see what you mean. This helps! 1 more question, do the images need to be the size of the UINavigationBar width depending on the screen size or it compiles/resizes based on the screen size? – Lukesivi Dec 01 '15 at 10:57
  • I am glad that it was helpful for you :) – swiftBoy Dec 01 '15 at 10:58
  • Thanks. Any insight on the sizing part? – Lukesivi Dec 01 '15 at 11:02
  • actually I'm still having a problem with the iPhone 5. I just noticed it. The iPhone 6 and the iPhone 6+ both work fine. But the iPhone 5 is the one giving me a problem. @RDC – Lukesivi Dec 01 '15 at 12:25