28

What is the correct way to color the UIImagePickerController's nav bar?
I merely tried to see the background color but I'm getting a faded color as seen in the image below; as if some view is obstructing it.

let picker = UIImagePickerController()
picker.sourceType = type
picker.mediaTypes = [kUTTypeImage]
picker.delegate = self
picker.navigationBar.backgroundColor = UIColor.redColor()

enter image description here

It appears to have some view obscuring the redColor():

(lldb) po picker.navigationBar.subviews
2 values
 {
  [0] = 0x00007fe7bb52a890
  [1] = 0x00007fe7bb52b670
}

What is the correct way to create a solid color for the Navigation Bar?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105

8 Answers8

58

Updated for Swift 4.2

For completeness, I'll add full color customization setup:

let imagePicker = UIImagePickerController()
imagePicker.navigationBar.isTranslucent = false
imagePicker.navigationBar.barTintColor = .blue // Background color
imagePicker.navigationBar.tintColor = .white // Cancel button ~ any UITabBarButton items
imagePicker.navigationBar.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white
] // Title color

which results in:

enter image description here

rbaldwin
  • 4,581
  • 27
  • 38
Michal
  • 15,429
  • 10
  • 73
  • 104
  • i tried you code it is working fine but cancel button is not showing – Satheeshkumar Naidu Dec 19 '18 at 07:49
  • 19
    This seems to be no longer working in iOS 13 -- the title, background and button colors are all the system colors. In iOS 13 the only way I could change the image picker navigation bar colors is via UINavigationBar.appearance().tintColor = UIColor.white etc. However, this isn't an ideal solution as it changes it for all the navigation bars. – tomblah Oct 11 '19 at 06:25
  • 1
    tomblah's comment is still applicable in iOS 14 with the PHPickerViewController. I just make sure to set the tint color back to its previous value after dismissing the picker. `let startingColor = UINavigationBar.appearance().tintColor; UINavigationBar.appearance().tintColor = .white` and then on dismiss `UINavigationBar.appearance().tintColor = startingColor` – Nathan Dudley Jun 15 '21 at 03:07
20

Try:

picker.navigationBar.translucent = false
picker.navigationBar.barTintColor = .redColor()

Instead of

picker.navigationBar.backgroundColor = UIColor.redColor()

If you want translucent effects, leave translucent = true as default.

rintaro
  • 51,423
  • 14
  • 131
  • 139
10

Here the right solution code in Objective-C. Might be useful.

imagePickerController.navigationBar.translucent = NO;
imagePickerController.navigationBar.barTintColor = [UIColor colorWithRed:0.147 green:0.413 blue:0.737 alpha:1];
imagePickerController.navigationBar.tintColor = [UIColor whiteColor];
imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
Walter
  • 196
  • 2
  • 4
9

Swift = IOS 8 || 9

Just put this method

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) 
{
     imagePicker.navigationBar.tintColor = .whiteColor()
     imagePicker.navigationBar.titleTextAttributes = [
     NSForegroundColorAttributeName : UIColor.whiteColor()
     ]

}
Mitul Marsoniya
  • 5,272
  • 4
  • 33
  • 57
7

Swift 5 / IOS 13

I have found this solution:

let barApperance = UINavigationBar.appearance()
barApperance.tintColor = .systemBlue

just put it where you create the UIImagePickerController()

user13968269
  • 71
  • 1
  • 1
3

For Swift, IOS 8-10 As rintaro mentioned, I think the main issue here is changing the default translucent property of the picker navigationBar:

picker.navigationBar.translucent = false

This will cause the the navigation bar to use the UINavigationBar appearance if you set this somewhere in your app.

If you need another color you can use
picker.navigationBar.barTintColor = UIColor.someColor

Boaz Frenkel
  • 618
  • 5
  • 15
0

UIImagePickerController is a UINavigationController. It can be styled as the same way the UINavigationController get styled.

hasan
  • 23,815
  • 10
  • 63
  • 101
0
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
[[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
     [UIColor whiteColor], nil]];

This works for me on iOS 14. Nothing else works.

Manish
  • 608
  • 1
  • 11
  • 23