ussd84
ussd84

Reputation: 99

Unable to inherit properties from base UIViewController class

I'm new to iOS development and this may be a simple question. My application will have many view controllers which will share similar properties. I created a base view controller class which subclasses UIViewController. All other view controllers subclass the base view controllers. However, the properties I set in base view controller class, are not inherited. For example, the navigation bar buttons should all be the color green yet are the default blue color when I build the app.

Here's the base view controller class:

class BaseViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.greenColor()
    self.navigationItem.rightBarButtonItem?.tintColor = UIColor.greenColor()
}

This is the code for other view controllers that subclass the base view controller above:

class AboutViewController: BaseViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = NSLocalizedString("About", comment: "About page title")
}

I've searched the web and SO for similar questions but wasn't able to find an answer. I don't see anything fundamentally wrong with my code. I am using Xcode 7.2.1 and testing on iOS 8+. Also tested in Xcode 7.2.3 beta but had similar results.

Upvotes: 2

Views: 872

Answers (1)

Korey Hinton
Korey Hinton

Reputation: 2562

From what I see, this isn't an inheritance issue. If you set a break point in viewDidLoad of BaseViewController it does indeed execute that code. I think your issue is with how to change the tint color. Customizing the navigation bar in iOS is such a pain that I usually stray away from using navigation controller and end up just creating my own top bar view I can fully customize.

If you are trying to change the tint color of both left and right bar items for your entire app put this code in your AppDelegate file inside the application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) function:

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

Here's a blog post I created that shows how to change the other colors of UINavigationBar: Navigation Bar Colors.

Upvotes: 2

Related Questions