我正在關注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