iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结
对于UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem这几种控件的自定义,因为具备共同性,因此放在一起讨论。
通常有两种方式来实现自定义。
1)获取控件的对象,然后对这个特定的对象进行特定的修改。
2)利用UIAppearance来实现对所有同类控件及特定同类的自定义。因为大多数应用里面的自定义为了美观,基本上相同类的控件自定义方式都一样,因此采用UIAppearance来使得界面的自定义变得非常方便。对于这种方式,通常在AppDelegate.m文件中实现,在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 这个方法内实现。
这里介绍一下UIAppearance的使用,参考自“UIAppearance protocol reference",有两种方式:
1、自定义所有类对象的显示。
[[UINavigationBar appearance] setTintColor:myColor];
2、自定义包含在特定containerclass 的类对象的显示。这主要针对UIBarButtonItem,就是说有的UIBarButtonItem在UINavigationBar中,有的在UIToolbar中,我们可以选择性的对存在于哪个bar中的button进行自定义。举例如下:
Customizing Appearance
backgroundImageForToolbarPosition:barMetrics:(page 7)
Returns the image to use for the background in a given position and with given metrics.
setBackgroundImage:forToolbarPosition:barMetrics:(page 7)
Sets the image to use for the background in a given position and with given metrics.
shadowImageForToolbarPosition:(page 9)
Returns the image to use for the toolbar shadow in a given position.
setShadowImage:forToolbarPosition:(page 9)
Sets the image to use for the toolbar shadow in a given position.
tintColor(page 6)property
The color used to tint the bar.
2、UITabBar
width(page 9)propertyThe width of the item.
customView(page 7)property
A custom view representing the item.
3、UINavigationBar
barStyle(page 7)property
The appearance of the navigation bar.
shadowImage(page 8)property
The shadow image to be used for the navigation bar.
translucent(page 10)property
A Boolean value indicating whether the navigation bar is only partially opaque.
基本上,把这些方法搞定,自定义就易如反掌了。