Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigationBar .tintColor with UIDocumentPickerViewController (Document Picker)?

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).

like image 518
simons Avatar asked Aug 26 '15 15:08

simons


Video Answer


2 Answers

You can alter navigationBar default tint color via UIAppearance:

UINavigationBar.appearance().tintColor = UIColor.orangeColor()
like image 130
rshev Avatar answered Oct 11 '22 03:10

rshev


swift 3,4 and xcode 9+ :

documentPicker.view.tintColor = .orange
like image 43
Ahmad Labeeb Avatar answered Oct 11 '22 04:10

Ahmad Labeeb