ios中uiappearance使用
2015-03-05来源:易贤网

ios5及其以后提供了一个比较强大的工具uiappearance,我们通过uiappearance设置一些ui的全局效果,这样就可以很方便的实现ui的自定义效果又能最简单的实现统一界面风格,它提供如下两个方法。

+ (id)appearance

这个方法是统一全部改,比如你设置uinavbar的tintcolor,你可以这样写:[[uinavigationbar appearance] settintcolor:mycolor];

+ (id)appearancewhencontainedin:(class <>)containerclass,...

这个方法可设置某个类的改变:例如:设置uibarbuttonitem 在uinavigationbar、uipopovercontroller、uitabbar中的效果。就可以这样写

[[uibarbuttonitem appearancewhencontainedin:[uinavigationbar class], [uipopovercontroller class],[uitabbar class] nil] settintcolor:mypopovernavbarcolor];

请注意*使用appearance设置ui效果最好采用全局的设置,在所有界面初始化前开始设置,否则可能失效。

支持uiappearance协议的类可以访问appearance selector ,它为receiver返回appearance proxy,我么可以给proxy发一些消息,诸如settintcolor:等

但是它并不是支持所有的ui类。下面列出它支持的类

1.uiactivitiindicatorview

2.uibarbuttonitem

3.uibaritem

4.uinavgationbar

5.uipopovercontroll

6.uiprogressview

7.uisearchbar

8.uisegmentcontroll

9.uislider

10.uiswitch

11.uitabbar

12.uitabbaritem

13.uitoolbar

14.uiview

15.uiviewcontroller

具体ui外观修改如下:

1.修改导航栏背景

代码如下:

uinavigationbar * appearance = [uinavigationbar appearance];

uiimage *navbackgroundimg =[uiimage imagenamed:@navbg.png”];

[appearance setbackgroundimage:navbackgroundimgforbarmetrics:uibarmetricsdefault];

2.标签栏(uitabbar)

代码如下:

uitabbar *appearance = [uitabbar appearance];

//设置背景图片

[appearance setbackgroundimage:[uiimage imagenamed:@tabbar_bg.png]];

//门置选择item的背景图片

uiimage * selectionindicatorimage =[[uiimageimagenamed:@tabbar_slider]resizableimagewithcapinsets:uiedgeinsetsmake(4, 0, 0,0)] ;

[appearance setselectionindicatorimage:selectionindicatorimage];

3.分段控件(uisegmentcontrol)

代码如下:

uisegmentedcontrol *appearance = [uisegmentedcontrol appearance];

//segmenteg正常背景

[appearance setbackgroundimage:[uiimage imagenamed:@segmente.png]

forstate:uicontrolstatenormal

barmetrics:uibarmetricsdefault];

//segmente选中背景

[appearance setbackgroundimage:[uiimage imagenamed:@segmente_a.png]

forstate:uicontrolstateselected

barmetrics:uibarmetricsdefault];

//segmente左右都未选中时的分割线

//barmetrics表示navigation bar的状态,uibarmetricsdefault 表示portrait状态(44pixel height),uibarmetricslandscapephone 表示landscape状态(32pixel height)

[appearance setdividerimage:[uiimage imagenamed:@segmente_line.png]

forleftsegmentstate:uicontrolstatenormal

rightsegmentstate:uicontrolstatenormal

barmetrics:uibarmetricsdefault];

[appearance setdividerimage:[uiimage imagenamed:@segmente_line.png]

forleftsegmentstate:uicontrolstateselected

rightsegmentstate:uicontrolstatenormal

barmetrics:uibarmetricsdefault];

[appearance setdividerimage:[uiimage imagenamed:@segmente_line.png]

forleftsegmentstate:uicontrolstatenormal

rightsegmentstate:uicontrolstateselected

barmetrics:uibarmetricsdefault];

//字体

nsdictionary *textattributes1 = @{uitextattributefont: [uifont systemfontofsize:18],

uitextattributetextcolor: [uicolor bluecolor],

uitextattributetextshadowcolor: [uicolor whitecolor],

uitextattributetextshadowoffset: [nsvaluevaluewithcgsize:cgsizemake(1, 1)]};

[appearance settitletextattributes:textattributes1 forstate:1];

nsdictionary *textattributes2 = @{uitextattributefont: [uifont systemfontofsize:18],

uitextattributetextcolor: [uicolor whitecolor],

uitextattributetextshadowcolor: [uicolor blackcolor],

uitextattributetextshadowoffset: [nsvaluevaluewithcgsize:cgsizemake(1, 1)]};

[appearance settitletextattributes:textattributes2 forstate:0];

4.uibarbutton

注意:uibarbutton有leftbarbutton,rightbarbutton和backbarbutton,其中backbarbutton由于带有箭头,需要单独设置。

barbutton背景设置是ios6.0及以后的,而backbutton是ios5.0及以后的,这里要注意!

代码如下:

//修改导航条上的uibarbuttonitem

uibarbuttonitem *appearance = [uibarbuttonitem appearancewhencontainedin:[uinavigationbar class], nil];

//设置导航栏的字体包括backbarbutton和leftbarbutton,rightbarbutton的字体

nsdictionary *textattributes = @{uitextattributefont: [uifontsystemfontofsize:18],

uitextattributetextcolor: [uicolorbluecolor],

uitextattributetextshadowcolor: [uicolorwhitecolor],

uitextattributetextshadowoffset: [nsvaluevaluewithcgsize:cgsizemake(1, 1)]};

[appearance settitletextattributes:textattributes forstate:1];//forstate为0时为下正常状态,为1时为点击状态。

//修改leftbarbutton,rightbarbutton背景效果

[appearance setbackgroundimage:[uiimage imagenamed:@navbarbutton.png]

forstate:uicontrolstatenormal

style:uibarbuttonitemstylebordered

barmetrics:uibarmetricsdefault];

[appearance setbackgroundimage:[uiimage imagenamed:@navbarbutton_a.png]

forstate:uicontrolstatehighlighted

style:uibarbuttonitemstylebordered

barmetrics:uibarmetricsdefault];

//backbarbutton需要单独设置背景效果。只能在ios6.0以后才能用

[appearance setbackbuttonbackgroundimage:[uiimage imagenamed:@nav_bg.png]

forstate:0

barmetrics:uibarmetricsdefault];

[appearance setbackbuttonbackgroundimage:[uiimage imagenamed:@work.png]

forstate:1

barmetrics:uibarmetricsdefault];

[appearance setbackbuttontitlepositionadjustment:uioffsetmake(2, -1)

forbarmetrics:uibarmetricsdefault];

5.工具栏(uitoolbar)

uitoolbar *appearance = [uitoolbar appearance];

//样式和背景二选一即可,看需求了

//样式(黑色半透明,不透明等)设置

[appearance setbarstyle:uibarstyleblacktranslucent];

//背景设置

[appearance setbackgroundimage:[uiimage imagenamed:@toolbarbg.png]

fortoolbarposition:uitoolbarpositionany

barmetrics:uibarmetricsdefault];

更多信息请查看IT技术专栏

推荐信息