mfaani
mfaani

Reputation: 36377

Customizing appearance of UISearchBar & UINavigationBar in AppDelegate? Why customize at class and not instance level?

I am following this tutorial. In its AppDelegate it has a customizeAppearance() where UISearchBar & UINavigationBar are type/class properties. Shouldn't they be a property of something like the window or the current viewController we are in?! How can we just message a class and then have it change our UI?

FWIW when I cmmd click...obviously it just takes it to the class definition.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var backgroundSessionCompletionHandler: (() -> Void)?
  var window: UIWindow?
  let tintColor =  UIColor(red: 242/255, green: 71/255, blue: 63/255, alpha: 1)

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    customizeAppearance()
    return true
  }

  func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
    backgroundSessionCompletionHandler = completionHandler
  }

  // MARK - App Theme Customization

  private func customizeAppearance() {
    window?.tintColor = tintColor
    UISearchBar.appearance().barTintColor = tintColor // shouldn't UISearchBar be a property of some other object?
    UINavigationBar.appearance().barTintColor = tintColor // shouldn't UINavigationBar be a property of some other object?
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
  }
}

Upvotes: 2

Views: 883

Answers (1)

dfrib
dfrib

Reputation: 73206

(I'll add my comments—answering the OP's question regarding class level customization—as an answer, as comments are not persistent. Possibly the OP himself/herself can add an alternative thorough answer based on trying out the queries discussed in the comments)


Quoting the language reference for UISearchBar:

Customizing Appearance

You can customize the appearance of search bars one at a time, or you can use the appearance proxy ([UISearchBar appearance]) to customize the appearance of all search bars in an app.

The appearance proxy is covered e.g. in the UIKit User Interface Catalog - About Views:

Appearance Proxies

You can use an appearance proxy to set particular appearance properties for all instances of a view in your application. For example, if you want all sliders in your app to have a particular minimum track tint color, you can specify this with a single message to the slider’s appearance proxy.

There are two ways to customize appearance for objects: for all instances and for instances contained within an instance of a container class.

...

As well as in the language reference for the UIAppearance protocol

Use the UIAppearance protocol to get the appearance proxy for a class. You can customize the appearance of instances of a class by sending appearance modification messages to the class’s appearance proxy.

...

  • To customize the appearance of all instances of a class, use appearance() to get the appearance proxy for the class.

In the tutorial you follow, they've chosen to use of the appearance proxy approach, making use of the static appearance() method as blueprinted in the UIAppearance protocol (to which e.g. UISearchBar conforms to, via UIView inheritance) to get and modify the appearance proxy of all UISearchBar (and UINavigationBar) instances, from a class level.


The following blog post covers the subject of the appearance proxy. An instructive read, even though being slightly outdated and using Obj-C rather than Swift:

Upvotes: 1

Related Questions