Customizing the look of UISearchBar and UINavigationBar in AppDelegate? Why configure at the class level, not at the instance level?

I follow this tutorial . In AppDelegate, it has customizeAppearance() , where UISearchBar and UINavigationBar are type / class properties. Shouldn't they be something like a window or the current viewController we're in ?! How can we just send a message to a class and then change its interface?

FWIW, when I press cmmd ..., it is obvious that it just goes 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()] } } 
+2
ios swift uinavigationbar uisearchbar
Dec 25 '16 at 20:31
source share
1 answer

(I will add my comments - answering the OP question regarding class level settings - as an answer, since the comments are not constant. Perhaps the OP itself can add an alternative comprehensive answer based on a poll of the queries discussed in the comment)




Quote language link for UISearchBar :

Custom setting

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

The appearance proxy is covered, for example. to UIKit User Interface Catalog - About Views :

Proxies

You can use the appearance proxy to set the specific appearance of the property for all instances of the view in the application . For example, if you want all the sliders in your application to have a minimum hue color, you can specify this with a single message to the proxy server with sliders.

There are two ways to customize the appearance for objects: for all instances, and instances contained in the instance container class.

...

As in the help system for the UIAppearance protocol

Use the UIAppearance protocol to get the appearance proxy for the class. You can configure the appearance of instances of the class to send messages about changes in appearance to the appearance of the proxy.

...

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

In the next tutorial, they decided to use the appearance proxy interface using the appearance() static method, as shown in the UIAppearance protocol (to which, for example, the UISearchBar corresponds, through the UIView inheritance), in order to obtain and change the appearance proxy for all instances UISearchBar (and UINavigationBar ) from class level.




The following blog post covers the theme of the appearance proxy. An instructive reading, although it is a bit outdated and uses Obj-C, not Swift:

+1
Dec 25 '16 at 21:25
source share




All Articles