simons simons - 2 months ago 16
Swift Question

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

Answer

You can alter navigationBar default tint color via UIAppearance:

UINavigationBar.appearance().tintColor = UIColor.orangeColor()