自定义 UISearchBar & 的外观AppDelegate 中的 UINavigationBar?为什么在类而不是实例级别进行自定义? [英] Customizing appearance of UISearchBar & UINavigationBar in AppDelegate? Why customize at class and not instance level?

查看:26
本文介绍了自定义 UISearchBar & 的外观AppDelegate 中的 UINavigationBar?为什么在类而不是实例级别进行自定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程.在它的 AppDelegate 中有一个 customizeAppearance() 其中 UISearchBar &UINavigationBar 是类型/类属性.它们不应该是我们所在的窗口或当前 viewController 之类的属性吗?!我们如何只给一个类发送消息,然后让它改变我们的 UI?

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 当我点击 cmmd 时...显然它只是将它带到类定义中.

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()]
  }
}

推荐答案

(我将添加我的评论——回答 OP 关于类级别定制的问题——作为答案,因为评论不是持久的.可能是 OP他/她自己可以根据尝试评论中讨论的查询添加替代的彻底答案)

引用 UISearchBar 的语言参考:

Quoting the language reference for UISearchBar:

自定义外观

您可以一次自定义一个搜索栏的外观,或者您可以使用外观代理([UISearchBar外观])来自定义应用中所有搜索栏的外观.

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.

外观代理被覆盖,例如在 UIKit 用户界面目录 - 关于视图:

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

外观代理

您可以使用外观代理来设置特定的外观应用程序中视图的所有实例的属性.为了例如,如果您希望应用程序中的所有滑块都具有特定的最小轨道色调颜色,您可以用一条消息指定它到滑块的外观代理.

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.

...

以及UIAppearance 协议的语言参考

As well as in the language reference for the UIAppearance protocol

使用 UIAppearance 协议获取一个外观代理班级.您可以通过以下方式自定义类实例的外观向类的外观发送外观修改消息代理.

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.

...

  • 要自定义一个类的所有实例的外观,使用appearance()来获取该类的外观代理.

在您遵循的教程中,他们选择使用外观代理方法,利用 UIAppearance 协议中的蓝图使用静态 appearance() 方法(例如UISearchBar符合,通过UIView继承)获取和修改所有UISearchBar(和UINavigationBar) 实例,来自类级别.

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.

以下博客文章涵盖了外观代理的主题.有启发性的阅读,尽管有些过时并且使用 Obj-C 而不是 Swift:

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:

这篇关于自定义 UISearchBar & 的外观AppDelegate 中的 UINavigationBar?为什么在类而不是实例级别进行自定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆