对于UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem这几种控件的自定义

来源:互联网 发布:finalcaption字幕软件 编辑:程序博客网 时间:2024/04/27 23:53
对于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进行自定义。举例如下:

[[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.注意下面的方法同样可以单独用于某个单独的对象。

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


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


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


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


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



除此之外,就是针对特定对象进行的自定义了。

1、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 选中及未选中的图像


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


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. 


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

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机接受信号不好怎么办 明天合格考了怎么办 和教育退订不了怎么办 舒张压降不下来怎么办 题目一行写不下怎么办 明细账写错了怎么办 生活太单调乏味怎么办 png图片不清晰怎么办 发票没写单位怎么办 右转车道直行了怎么办 注塑模具产品吸气怎么办 包装机理料线缺料不停怎么办 儿童小脑发育不好怎么办 普通税票弄坏了怎么办 肠胃不好人消瘦怎么办 子宫小发育不良怎么办 卵泡涨的慢怎么办 先天性子宫发育不良怎么办 签证出生日期错了怎么办 车启动开不动怎么办 etc显示无效卡怎么办 最小化只能保留一个怎么办 word句号在中间怎么办 搜狗输入法打不出字怎么办 台式电脑键盘打不出字怎么办 键盘挨锁了怎么办 名牌包过安检怎么办 和地铁安检员冲突怎么办 乘客不配合安检怎么办? 富人移民了穷人怎么办 感觉自己太帅怎么办 上海街头艺人证怎么办 婆婆死了带孩子怎么办 婆婆不带孩子怎么办 空腹吃李子了怎么办 唱歌不在调上怎么办 彩色衣服变黄怎么办 小学生学习态度不好怎么办 孕晚期血小板低怎么办 情侣空间别人不同意怎么办 手机屏幕进了水怎么办