ios5及其以后提供了一個比較強大的工具uiappearance,我們通過uiappearance設(shè)置一些ui的全局效果,這樣就可以很方便的實現(xiàn)ui的自定義效果又能最簡單的實現(xiàn)統(tǒng)一界面風(fēng)格,它提供如下兩個方法。
+ (id)appearance
這個方法是統(tǒng)一全部改,比如你設(shè)置uinavbar的tintcolor,你可以這樣寫:[[uinavigationbar appearance] settintcolor:mycolor];
+ (id)appearancewhencontainedin:(class <>)containerclass,...
這個方法可設(shè)置某個類的改變:例如:設(shè)置uibarbuttonitem 在uinavigationbar、uipopovercontroller、uitabbar中的效果。就可以這樣寫
[[uibarbuttonitem appearancewhencontainedin:[uinavigationbar class], [uipopovercontroller class],[uitabbar class] nil] settintcolor:mypopovernavbarcolor];
請注意*使用appearance設(shè)置ui效果最好采用全局的設(shè)置,在所有界面初始化前開始設(shè)置,否則可能失效。
支持uiappearance協(xié)議的類可以訪問appearance selector ,它為receiver返回appearance proxy,我么可以給proxy發(fā)一些消息,諸如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.修改導(dǎo)航欄背景
代碼如下:
uinavigationbar * appearance = [uinavigationbar appearance];
uiimage *navbackgroundimg =[uiimage imagenamed:@navbg.png”];
[appearance setbackgroundimage:navbackgroundimgforbarmetrics:uibarmetricsdefault];
2.標(biāo)簽欄(uitabbar)
代碼如下:
uitabbar *appearance = [uitabbar appearance];
//設(shè)置背景圖片
[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的狀態(tài),uibarmetricsdefault 表示portrait狀態(tài)(44pixel height),uibarmetricslandscapephone 表示landscape狀態(tài)(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有l(wèi)eftbarbutton,rightbarbutton和backbarbutton,其中backbarbutton由于帶有箭頭,需要單獨設(shè)置。
barbutton背景設(shè)置是ios6.0及以后的,而backbutton是ios5.0及以后的,這里要注意!
代碼如下:
//修改導(dǎo)航條上的uibarbuttonitem
uibarbuttonitem *appearance = [uibarbuttonitem appearancewhencontainedin:[uinavigationbar class], nil];
//設(shè)置導(dǎo)航欄的字體包括backbarbutton和leftbarbutton,rightbarbutton的字體
nsdictionary *textattributes = @{uitextattributefont: [uifontsystemfontofsize:18],
uitextattributetextcolor: [uicolorbluecolor],
uitextattributetextshadowcolor: [uicolorwhitecolor],
uitextattributetextshadowoffset: [nsvaluevaluewithcgsize:cgsizemake(1, 1)]};
[appearance settitletextattributes:textattributes forstate:1];//forstate為0時為下正常狀態(tài),為1時為點擊狀態(tài)。
//修改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需要單獨設(shè)置背景效果。只能在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];
//樣式和背景二選一即可,看需求了
//樣式(黑色半透明,不透明等)設(shè)置
[appearance setbarstyle:uibarstyleblacktranslucent];
//背景設(shè)置
[appearance setbackgroundimage:[uiimage imagenamed:@toolbarbg.png]
fortoolbarposition:uitoolbarpositionany
barmetrics:uibarmetricsdefault];
更多信息請查看IT技術(shù)專欄