iOS 5:对 UIAppearance 感到好奇

时间:2023-03-25
本文介绍了iOS 5:对 UIAppearance 感到好奇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 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 with UI_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 感到好奇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:iOS 5 中的单例? 下一篇:如何在情节提要中的 UIViewControllers 之间手动切换?

相关文章