問題描述
在 2011 年技術演講中,我看到了一個關于新 UIAppearance
協議.那里還沒有很多文檔.我將簡短地總結一下我記得的內容,以解釋我的問題來自哪里:
On the tech talk 2011 I saw a speech about the new UIAppearance
protocol. There is not a lot of documentation out there yet. I will shortly summarize what I remember to explain where my question is coming from:
關于UIAppearance
:
到目前為止,您必須在所有應用視圖控制器中自定義每個導航欄(工具欄等)屬性.使用新的外觀屬性,您可以只在一個地方為應用程序中的所有導航欄執行此操作.例如:如果您想自定義導航欄背景圖像,您可以這樣做:
So far you had to customize EVERY navigation bar (toolbar, etc.) property in all your app view controllers. With the new appearance property you can do it in only one place for all navigation bars in your app. For instance: if you want to customize your Navigation bar background image you could do it like this:
[[UINavigationBar appearance] setBackgroundImage:
[UIImage imageNamed:@"MyImageName"] forBarMetrics:UIBarMetricsDefault];
這將設置應用程序中所有導航欄的背景圖像.使用 barMetrics
您可以指定是否要在橫向模式下也使用圖像.
This will set the background image of ALL navigation bars within your application. With the barMetrics
you specify whether you want the image to be used also in landscape mode or not.
如果您想指定僅在某些視圖控制器中應用的外觀,文檔中還有一種方法可以通過指定對象的位置來控制它:
If you want to specify the appearance to be applied only in some view controllers there is also a method in the docs to control that by specifying where your objects are located:
[[UIBarButtonItem appearanceWhenContainedIn:
[ABPeoplePickerNavigationController class], nil] setTintColor:myNavBarColor];
另外值得一提的是,如果您有單個自定義實例,與您的外觀設置不同,這些實例將不受外觀代理的影響.
Also worth to mention is, if you have single customized instances, differing from your appearance settings, theses instances will not be effected by the appearance proxy.
一些問題:
a) 我如何知道類的哪些屬性與外觀屬性一起使用? 例如因為 UITableView
符合 UIAppearance 協議,所以我想我可以做類似的事情
a) How do I know which properties of a class work with the appearance property? For e.g. since UITableView
conforms to the UIAppearance protocol I was thinking I could do something like
[[UITableView appearance] setBackgroundColor:mytableViewColor];
來操縱我所有表格視圖的背景顏色,但我不能!
to manipulate the background color of all my table views, but I can't!
b) 是否有所有可使用外觀屬性操作的類的列表?
c) 在什么時候調用外觀自定義? 我希望在運行時對外觀屬性進行更改,但不幸的是,這些更改沒有發生.
c) At what point is the appearance customization being called? I was hoping to make changes threw the appearance property at runtime, but unfortunately the changes aren't taking place.
推薦答案
a) 我如何知道某個類的哪些實例與外觀屬性一起使用?例如因為 UITableView 符合 UIAppearance 協議,所以我想我可以做類似的事情
a) How do I know which instances of a class work with the appearance property? For e.g. since UITableView conforms to the UIAppearance protocol I was thinking I could do something like
您查看類的標題(以及所有超類的標題).UI_APPEARANCE_SELECTOR
旁邊的任何方法都支持與 UIAppearance
代理一起使用.
You look in the header of the class (and the headers of all the superclasses). Any method that has UI_APPEARANCE_SELECTOR
next to it is supported for use with the UIAppearance
proxy.
[[UITableView appearance] setBackgroundColor:mytableViewColor];
backgroundColor
屬性未使用 UIView.h
中的 UI_APPEARANCE_SELECTOR
修飾.因此,它在技術上不支持與外觀代理一起使用.它可能會起作用,但(鑒于缺乏方法修飾)不能保證.
The backgroundColor
property is not decorated with UI_APPEARANCE_SELECTOR
in UIView.h
. Thus it is not technically supported for use with the appearance proxy. It will probably work, but (given the lack of method decoration) isn't guaranteed to.
來自UIAppearance 協議參考
:
要支持外觀定制,類必須符合UIAppearanceContainer
協議,相關訪問器方法必須用UI_APPEARANCE_SELECTOR
標記.
To support appearance customization, a class must conform to the
UIAppearanceContainer
protocol and relevant accessor methods must be marked withUI_APPEARANCE_SELECTOR
.
(注意和相關的訪問器方法必須標記..."[強調添加])
(note "and relevant accessor methods must be marked..." [emphasis added])
b) 是否有所有可使用外觀屬性進行操作的屬性的列表?
b) Is there a list of all properties that are manipulatable with the appearance property?
是否有一個頁面顯示每個使用外觀代理的設置器?我不知道,也沒有辦法在運行時構建列表.
Is there a single page showing every setter that works with the appearance proxy? I don't know of one, nor is there a way to build the list at runtime.
c) 在什么時候調用外觀定制?我希望在運行時對外觀屬性進行更改,但不幸的是沒有發生更改.
c) At what point is the appearance customization being called? I was hoping to make changes threw the appearance property at runtime, but unfortunately the changes aren't taking place.
您可以在執行期間的任何時候使用外觀代理.直到下次調用這些視圖的 -layoutSubviews
方法時,這些更改才會應用于受影響的視圖.
You can use the appearance proxy at any point during execution. The changes won't be applied to the affected views until the next time those views have their -layoutSubviews
method invoked.
這篇關于iOS 5:對 UIAppearance 感到好奇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!