15
votes

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

4
use UINavigationBar.appearance().bartintColor = .greenAnbu.Karthik
show some additional codeAnbu.Karthik
When I set barTintColor it changes color of root view controller. No effect at document picker.Kryštof Matěj
try with inside the completion onceAnbu.Karthik
I added UINavigationBar.appearance().barTintColor = .green to completion and It makes no effect.Kryštof Matěj

4 Answers

34
votes

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
}
4
votes

Use setTitleTextAttributes

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

see my answer here

1
votes

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
votes

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