18

My app is using red navigation (2) bar with white buttons and texts. When I use system contact picker (3) the status bar is red. When I use documents picker (1) UIDocumentPickerViewController then navigation bar is white. How I can change color of navigation bar or text?

When I use code bellow, it works but it change my navigation bar too.

UINavigationBar.appearance().tintColor = .red

thanks for help

code:

func open() {
        UINavigationBar.appearance().barTintColor = .green
        let documentsController = UIDocumentPickerViewController(documentTypes: makeDocumentTypesList(), in: .import)
        documentsController.delegate = self
        viewControllerProvider.getViewController().present(documentsController, animated: true, completion: nil)
    }

navigation bar

Kryštof Matěj
  • 462
  • 3
  • 16

4 Answers4

41

You can just reset the appearance for UIDocumentPickerViewController only by putting this code somewhere in your application:didFinishLaunchingWithOptions: function and the bar buttons will return to their original blue or you can set any other color of your choice. The bar color, on the other hand is not customizable.

if #available(iOS 11.0, *) {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil
}
Antoine Lamy
  • 863
  • 10
  • 9
  • is this only for iOS 11.0 and above? – Ankit Kumar Gupta Nov 26 '18 at 13:40
  • 1
    This is the solution for me, i have set tint color to white and in the UIDocumentBrowserViewController couldnt see the `cancel` button. now i have set it to blue. `UINavigationBar.appearance().tintColor = UIColor.white if #available(iOS 11.0, *) { UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = UIColor.blue }` – Im Batman Mar 22 '19 at 11:08
  • 1
    Hi, I found your answer to work fine as you described with UIDocumentBrowserViewController but I can't get it to work with UIImagePickerController - do you have any ideas here? – Paweł Zgoda-Ferchmin Sep 07 '20 at 11:01
4

Use setTitleTextAttributes

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .normal)

see my answer here

Developeder
  • 1,579
  • 18
  • 29
1

This is the only solution I found:

    UINavigationBar.appearance().tintColor = ... some color
    _viewController?.present(documentPicker, animated: true)

To my knowledge it is not possible to set the bar color only the tint color (text color). To keep the tint color for the rest of your project I reset it in the viewWillAppear of the underlying view controller.

-2

as you ask "change color of navigation bar or text?" i haven't a solution for change navigation bar it always return nil

self.present(documentPicker, animated: true,completion: {

                if documentPicker.navigationController?.navigationBar != nil{
                    documentPicker.navigationController?.navigationBar.barTintColor = .red
                }
            })

but if you agree to change only text

this works for me

self.present(documentPicker, animated: true,completion: {

                documentPicker.view.tintColor = .red
            })

i understand that it couldn't be the optimal but none of solutions i try work for me

Ahmad Labeeb
  • 1,056
  • 11
  • 21