iOS 開發 中級:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定義方法總結

原文:  http://blog.csdn.net/songrotek/article/details/8692866?utm_source=tuicoolapp

 

 

對於UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem這幾種控件的自定義,由於具有共同性,所以放在一塊兒討論。ui

一般有兩種方式來實現自定義。.net

1)獲取控件的對象,而後對這個特定的對象進行特定的修改。對象

2)利用UIAppearance來實現對全部同類控件及特定同類的自定義。由於大多數應用裏面的自定義爲了美觀,基本上相同類的控件自定義方式都同樣,所以採用UIAppearance來使得界面的自定義變得很是方便。對於這種方式,一般在AppDelegate.m文件中實現,在blog

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 這個方法內實現。ci

 

這裏介紹一下UIAppearance的使用,參考自「UIAppearance protocol reference",有兩種方式:it

一、自定義全部類對象的顯示。io

[[UINavigationBar appearance] setTintColor:myColor];class

二、自定義包含在特定containerclass 的類對象的顯示。這主要針對UIBarButtonItem,就是說有的UIBarButtonItem在UINavigationBar中,有的在UIToolbar中,咱們能夠選擇性的對存在於哪一個bar中的button進行自定義。舉例以下:sed

 

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]setTintColor:myNavBarColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],[UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] setTintColor:myToolbarColor];

 

 

 

下面總結一下各控件使用UIAppearance自定義的Methods。摘自各蘋果官方的Reference.注意下面的方法一樣能夠單獨用於某個單獨的對象。

一、UIToolbar

 

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. 

 

二、UITabBar

 

Customizing Appearance

backgroundImage (page 5)propertyThe background image for the bar.

selectedImageTintColor (page 7) property
The tint color to apply to the gradient image used when creating the selected image.

selectionIndicatorImage (page 8) propertyThe image used for the selection indicator.

shadowImage (page 8)property
The shadow image to be used for the tab bar.

tintColor (page 8)property
The tint color to apply to the tab bar background. 

 

 

三、UINavigationBar

 

Customizing the Bar Appearance

tintColor (page 9)property
The color used to tint the bar.

–backgroundImageForBarMetrics:(page11)
Returns the background image for given bar metrics.

–  setBackgroundImage:forBarMetrics:(page 13)Sets the background image for given bar metrics.

–  titleVerticalPositionAdjustmentForBarMetrics:(page 14)Returns the title’s vertical position adjustment for given bar metrics.

–  setTitleVerticalPositionAdjustment:forBarMetrics:(page 14)Sets the title’s vertical position adjustment for given bar metrics.

titleTextAttributes (page 9) propertyDisplay attributes for the bar’s title text. 

 

 

四、UIBarButtonItem

 

Customizing Appearance

tintColor(page 9) property
The tint color for the button item.

–  backButtonBackgroundImageForState:barMetrics:(page 10)

Returns the back button background image for a given control state and bar metrics.

–  setBackButtonBackgroundImage:forState:barMetrics:(page 17)

Sets the back button background image for a given control state and bar metrics

–backButtonTitlePositionAdjustmentForBarMetrics:(page11)Returns the back button title offset for given bar metrics.

– setBackButtonTitlePositionAdjustment:forBarMetrics:(page 19)Sets the back button title offset for given bar metrics

– backButtonBackgroundVerticalPositionAdjustmentForBarMetrics:(page 10)Returns the back button vertical position offset for given bar metrics.

– setBackButtonBackgroundVerticalPositionAdjustment:forBarMetrics:(page 18)Sets the back button vertical position offset for given bar metrics.

– backgroundVerticalPositionAdjustmentForBarMetrics:(page 13)Returns the background vertical position offset for given bar metrics.

– setBackgroundVerticalPositionAdjustment:forBarMetrics:(page 21)Sets the background vertical position offset for given bar metrics.

–backgroundImageForState:barMetrics:(page11)

Returns the background image for a given state and bar metrics.

– setBackgroundImage:forState:barMetrics:(page 19)

Sets the background image for a given state and bar metrics.

– backgroundImageForState:style:barMetrics:(page 12)

Returns the background image for the specified state, style, and metrics.

– setBackgroundImage:forState:style:barMetrics:(page 20)

Sets the background image for the specified state, style, and metrics.

– titlePositionAdjustmentForBarMetrics:(page 22)Returns the title offset for given bar metrics.

– setTitlePositionAdjustment:forBarMetrics:(page 21)Sets the title offset for given bar metrics. 

 

 

五、UITabBarItem

 

Customizing Appearance

– titlePositionAdjustment(page 8)
Returns the offset to use to adjust the title position.

– setTitlePositionAdjustment:(page 8)
Sets the offset to use to adjust the title position. 

 

 

 

除此以外,就是針對特定對象進行的自定義了。

一、UITabBarItem

 

– finishedSelectedImage (page 5)Returns the finished selected image.

– finishedUnselectedImage(page 5)Returns the finished unselected image.

– setFinishedSelectedImage:withFinishedUnselectedImage:(page 7)Sets the finished selected and unselected images. 

上面的方法用於更改每一個單獨的TabBarItem 選中及未選中的圖像

 

 

二、UIBarButtonItem

 

style (page 8)propertyThe style of the item.

possibleTitles (page 7)property
The set of possible titles to display on the bar button.

page5image13240
page5image13512
page5image13784

width (page 9)propertyThe width of the item.

customView (page 7)property
A custom view representing the item. 

 

 

三、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. 

 

 

基本上,把這些方法搞定,自定義就易如反掌了。

相關文章
相關標籤/搜索