I have figured out how to set a Navigation Controller's Navigation Bar to a custom class via a Storyboards https://stackoverflow.com/a/42989418/47281. How can you do this programmatically with Swift?
Asked
Active
Viewed 1,568 times
1 Answers
2
Create a subclass of UINavigationBar
class PrettyNavigationBar: UINavigationBar {
}
Then create your UINavigationController with your custom UINavigationBar:
let navigationController = UINavigationController(navigationBarClass: PrettyNavigationBar.self, toolbarClass: nil)
If you're using a storyboard:
Select your navigationBar in your Interface Builder. Go to the identity inspector in the right pane and set the class like this:

Hapeki
- 2,153
- 1
- 20
- 36
-
Thanks - we did create the subclass. But our controller is getting instantiate via a Storyboard. See comment on Eendje's answer. – Marcus Leon Mar 24 '17 at 10:25
-
Right, that's what we're currently doing... looking to set it programattically once the Storyboard creates the Nav Controller. – Marcus Leon Mar 24 '17 at 10:33
-
What problem are you trying to solve with this approach? It seems a bit odd to create a Nav Controller without a navBar and then set it in a custom init. – Hapeki Mar 24 '17 at 10:41
-
Yah maybe this is silly. I use Storyboard's so I can see the general layout of our screens but I like to keep the configuration in the code so it's more explicit as to what I've changed. But in this case maybe I should just set the navbar class in the Storyboard. – Marcus Leon Mar 24 '17 at 11:25
-
Your approach is how I make my apps. Semi storyboards for the visuals, code for the changes. If you're doing this "hybrid" approach I'd recommend going for xibs/nibs, each xib can have it's own View class with all the outlets from the XIB. In this view class you can configure your views programmatically, although for UINavigationController it's always cumbersome. – Hapeki Mar 24 '17 at 12:01