iOS 5:好奇UIAppearance [英] iOS 5: Curious about UIAppearance

查看:129
本文介绍了iOS 5:好奇UIAppearance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2011年的技术演讲中,我看到了关于新的 UIAppearance 协议。目前还没有很多文档。我会很快总结一下我的问题是从哪里来解释的:






关于 UIAppearance



到目前为止,你必须在所有应用程序中自定义每个导航栏视图控制器。有了新的外观属性,你可以在一个地方为您的应用程序中的所有导航栏。例如:如果你想自定义导航栏背景图片,你可以这样做:

  [[UINavigationBar appearance] setBackgroundImage :
[UIImage imageNamed:@MyImageName] forBarMetrics:UIBarMetricsDefault];

这将设置应用程序中所有导航栏的背景图像。使用 barMetrics ,您可以指定是否要让图片以横向模式使用。



想要指定仅在一些视图控制器中应用的外观,在文档中还有一个方法通过指定对象所在的位置来控制:

  [[UIBarButtonItem appearanceWhenContainedIn:
[ABPeoplePickerNavigationController class],nil] setTintColor:myNavBarColor];另外值得一提的是,如果你有一个自定义的实例,与你的外观设置不同,这些实例将不会受到外观代理的影响。






一些问题:



a)如何知道类的哪些属性使用appearance属性?对于eg因为 UITableView 符合UIAppearance协议我认为我可以做像

  [[UITableView appearance] setBackgroundColor:mytableViewColor]; 

来操作我所有表视图的背景颜色,但我不能!



b)是否有可以使用外观属性操作的所有类的列表?



c)外观定制被调用的点是什么?我希望在运行时更改外观属性,但不幸的是,更改不会发生。

解决方案


a)如何知道类的哪些实例使用appearance属性?例如,因为UITableView符合UIAppearance协议,我想我可以做一些像


你看看类的标题的所有超类)。任何在其旁边具有 UI_APPEARANCE_SELECTOR 的方法都支持与 UIAppearance 代理一起使用。

  [[UITableView外观] setBackgroundColor:mytableViewColor]; 

backgroundColor 属性未使用 UIView.h 中的code> UI_APPEARANCE_SELECTOR 。因此,在技术上不支持与外观代理一起使用。



可以 工作,但是(由于缺少方法修饰) UIAppearance协议参考


为了支持外观定制,类必须符合 UIAppearanceContainer 协议和相关的访问器方法必须标记为 UI_APPEARANCE_SELECTOR


(注意相关的访问器方法必须必须标记为...[添加重点])


b)是否有可以使用appearance属性操作的所有属性的列表?


是否有一个页面显示每个与外观代理一起使用的setter?我不知道一个,也没有办法在运行时创建列表。







c)外观定制在什么时候被调用?我希望在运行时改变外观属性,但不幸的是,更改不会发生。


您可以使用外观代理在任何时候执行。更改不会应用到受影响的视图,直到下次这些视图调用 -layoutSubviews 方法。


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:


About 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]; 

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.


Some questions:

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) Is there a list of all classes that are manipulatable with the appearance property?

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) 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

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];

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.

From the UIAppearance Protocol Reference:

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) 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) 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.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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