我正在关注this tutorial。在它的AppDelegate中它有一个customizeAppearance()
,其中UISearchBar
& UINavigationBar
是类型/类属性。它们不应该是窗口或我们所在的当前viewController之类的属性吗?!我们如何才能发布一个类,然后让它改变我们的UI?在AppDelegate中自定义UISearchBar&UINavigationBar的外观?为什么要在课堂而不是实例级别自定义?
FWIW当我cmmd点击...显然它只是把它带到类的定义。
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()]
}
}
引用[用于'UISearchBar'语言参考](https://developer.apple.com/reference/uikit/uisearchbar):_“您可以一次自定义搜索栏的外观,或者您可以使用外观代理('[UISearchBar appearance]')自定义应用程序中所有搜索栏的外观。 “_。在你接下来的教程中,他们选择了后者的代理方法,利用[UIAppearance'协议]中的蓝图的静态'appearance()'方法(https://developer.apple.com/reference/ uikit/uiappearance)例如'UISearchBar'符合(通过'UIView'继承)。 – dfri
@dfri ohh。哇。这很方便。所以,如果我一次更换一个,并使用外观进行自定义。代理方法将被覆盖权利?也是这种*代理*机制在其他地方提供的可可触摸或不是? – Honey
我不能说没有测试,但进一步引用语言参考。对于'UIAppearance':_“iOS在视图进入窗口时应用外观变化,它不会改变已经在窗口中的视图的外观......”_以及_“在任何给定的视图层次结构中,最外层的外观代理获胜。“_。所以我相信取决于您选择自定义外观的位置(或深度),默认/自定义代理行为(在视图进入窗口时加载)或您的自定义实例集行为将优先。看看参考文献。 docs for'UIAppearance' for det.s. – dfri