5

I am using Swift 2 in Xcode 7.0 beta 6

Long story short, I am trying to work out how to set .navigationBar.barStyle and navigationBar.tintColor when using a document picker to access iCloud - i.e. a UIDocumentPickerViewController.

I have tried e.g. :

/...
documentPicker.navigationController!.navigationBar.barStyle = UIBarStyle.Default
documentPicker.navigationController!.navigationBar.tintColor = UIColor.orangeColor()
/...

For example. Here I have a view controller embedded within a navigation controller:

enter image description here

In MyNavigationController I can set the .barStyle and .tintStyle as follows:

class MyNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.barStyle = UIBarStyle.Default
        self.navigationBar.tintColor = UIColor.orangeColor()
    }
}

So .tintStyle is orange as follows:

enter image description here

iCloud is enabled and FirstViewController conforms to UIDocumentPickerDelegate. The bar button calls an IBAction function as shown here in the code for FirstViewController:

class FirstViewController: UIViewController, UIDocumentPickerDelegate {
    // ...
    @IBAction func importDocument(sender: UIBarButtonItem) {
        let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], inMode: UIDocumentPickerMode.Import)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
        documentPicker.popoverPresentationController?.barButtonItem = sender
        self.presentViewController(documentPicker, animated: true, completion: nil)
}

    func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
        // ...
    }

    func documentPickerWasCancelled(controller: UIDocumentPickerViewController) {
        // ...
    }
}

That works. The document picker loads as expected:

enter image description here

BUT. For the sake of working out how to do this, I want the menu item "Done" to be orange. Like the previous.

I have tried adding the following code to the @IBAction as follows:

//...
documentPicker.navigationController!.navigationBar.barStyle = UIBarStyle.Default
documentPicker.navigationController!.navigationBar.tintColor = UIColor.orangeColor()
self.presentViewController(documentPicker, animated: true, completion: nil)
//...

That doesn't work because at this point documentPicker.navigationController is nil.

Can anyone tell me how or where in the cycle I can access documentPicker.navigationController!.navigationBar.tintColor?

Or perhaps I am missing something and there is some other way of changing the menu color?

Or perhaps I should be looking to create a custom navigation controller - and a custom document picker view controller. Then in theory I would be able to access a relevant viewDidLoad. I tried that but realised that I would then also need a custom version of the UIDocumentPickerDelegate protocol. There must surely be an easier solution (and I had doubts as to whether that would be allowed).

2 Answers 2

4

You can alter navigationBar default tint color via UIAppearance:

UINavigationBar.appearance().tintColor = UIColor.orangeColor()
1
  • 1
    This will change whole application tint color can we change just documentViewController tint color Feb 1, 2019 at 8:43
1

swift 3,4 and xcode 9+ :

documentPicker.view.tintColor = .orange
7
  • its changing view items tint color not bar button tint color. Mar 16, 2018 at 6:47
  • I know but in document picker there are no navigation bar , items are subviews of view so it resolves the problem Mar 16, 2018 at 20:44
  • Naah it's not working . View tint color will apply on it's subview. Above answer works fine. Mar 19, 2018 at 6:25
  • my code works for me not above answer , may be it is different in IOS version , if you found the answer why tell me Mar 19, 2018 at 10:09
  • Using this approach will change the tint for the tab bar, search bar, and navigation bar within the document picker.
    – Paul Solt
    Oct 22, 2019 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.