7

I want to set white barTintColor in some viewcontroller I have set UINavigationBar.appearance().barTintColor for all default color but when I use appearance whenContainedInInstancesOf It's not change my viewcontroller

UINavigationBar.appearance(whenContainedInInstancesOf: [MyViewController.self]).barTintColor = .white

Any idea?. I tried this in my viewcontroller

self.navigationcontroller.navigationbar.barTintColor = .white

but I have to set color back to default when screen will disappear. I don't want to do like that. How can I do?

Oniikal3
  • 567
  • 1
  • 6
  • 14
  • Possible duplicate of [appearanceWhenContainedIn in Swift](http://stackoverflow.com/questions/24136874/appearancewhencontainedin-in-swift) – zombie Feb 22 '17 at 21:09
  • 2
    @zombie I tried this `UINavigationBar.appearance(whenContainedInInstancesOf: [MyViewController.self]).barTintColor = .white` but it still not work! – Oniikal3 Feb 22 '17 at 21:23
  • 1
    But it says that it's only for iOS 10 so which version are you testing on – zombie Feb 22 '17 at 21:25
  • @zombie my device is 10.1.1. – Oniikal3 Feb 22 '17 at 21:29

4 Answers4

8

UINavigationBar is contained in a UINavigationController not UIViewController

with that said you need to create a custom UINavigationController

an empty class will do the job

class NavigationController: UINavigationController {}

then we can use it

UINavigationBar.appearance(whenContainedInInstancesOf: [NavigationController.self]).barTintColor = .white

Example can be found here

zombie
  • 5,069
  • 3
  • 25
  • 54
  • I think this will change others screen too. which screens are next to first screen (screen that's need white navigationbar). – Oniikal3 Feb 22 '17 at 22:09
  • if you mean that this will change the color for `UINavigationController` too then no. it will take effect only for `NavigationController` – zombie Feb 22 '17 at 22:12
  • @Oniikal3 by the way did you think or try? – zombie Feb 22 '17 at 22:20
  • Yeah, I tried. It's show as I think. It's change all screen that's in flow. Now I have to set it in each viewcontroller. I don't want to struck on it. Thanks for your solutions :) – Oniikal3 Feb 23 '17 at 07:32
  • @Oniikal3 that's strange anyway I added an example to take a look at. Please let me know if you still have a problem – zombie Feb 23 '17 at 08:26
  • This should be the correct answer, you saved my day. Thank you :) – paky Sep 04 '21 at 18:14
7

@zombie has the correct answer. I just came across this issue a moment ago with UISearchBar. Here's the Swift 3 version of the answer. Using .red as the sample UIColor for this example.

 let textField = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
 textField.tintColor = .red

 let searchBar = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
 searchBar.setTitleTextAttributes([NSForegroundColorAttributeName: .red], for: .normal)
 searchBar.setTitleTextAttributes([NSForegroundColorAttributeName: .red], for: .disabled)

Sample Image

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
3

Swift 4:

    // For the cursor color
    let textField = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
    textField.tintColor = .lightBlue

    // For the cancel button on search bar
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.lightText], for: .disabled)
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
0
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

This worked for me!

khush
  • 540
  • 5
  • 16