iOS5及其以后提供了一個(gè)比較強(qiáng)大的工具UIAppearance,我們通過(guò)UIAppearance設(shè)置一些UI的全局效果,這樣就可以很方便的實(shí)現(xiàn)UI的自定義效果又能最簡(jiǎn)單的實(shí)現(xiàn)統(tǒng)一界面風(fēng)格,它提供如下兩個(gè)方法。
+(id)appearance
這個(gè)方法是統(tǒng)一全部改,比如你設(shè)置UINavBar的tintColor,你可以這樣寫(xiě):[[UINavigationBarappearance] setTintColor:myColor];
+(id)appearanceWhenContainedIn:(Class<>)ContainerClass,...
這個(gè)方法可設(shè)置某個(gè)類的改變:例如:設(shè)置UIBarButtonItem在UINavigationBar、UIPopoverController、UITabbar中的效果。就可以這樣寫(xiě)
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBarclass], [UIPopoverController class],[UITabbar class] nil]setTintColor:myPopoverNavBarColor];
請(qǐng)注意*使用appearance設(shè)置UI效果最好采用全局的設(shè)置,在所有界面初始化前開(kāi)始設(shè)置,否則可能失效。
具體UI外觀修改如下:
1.修改導(dǎo)航欄背景
代碼如下:
UINavigationBar * appearance = [UINavigationBarappearance];
UIImage *navBackgroundImg =[UIImage imageNamed:@"navBg.png”];
[appearance setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
2.標(biāo)簽欄(UITabbar)
代碼如下:
UITabBar *appearance = [UITabBar appearance];
//設(shè)置背景圖片
[appearance setBackgroundImage:[UIImage imageNamed:@"tabbar_bg.png"]];
//門置選擇item的背景圖片
UIImage *selectionIndicatorImage =[[UIImage imageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4,0, 0,0)] ;
[appearance setSelectionIndicatorImage:selectionIndicatorImage];
3.分段控件(UISegmentControl)
代碼如下:
UISegmentedControl *appearance = [UISegmentedControlappearance];
//Segmenteg正常背景
[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente.png"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
//Segmente選中背景
[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente_a.png"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
//Segmente左右都未選中時(shí)的分割線
//BarMetrics表示navigationbar的狀態(tài),UIBarMetricsDefault表示portrait狀態(tài)(44pixelheight),UIBarMetricsLandscapePhone表示landscape狀態(tài)(32pixelheight)
[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:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};
[appearance setTitleTextAttributes:textAttributes1forState:1];
NSDictionary *textAttributes2 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor:[UIColor blackColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};
[appearance setTitleTextAttributes:textAttributes2forState:0];
4.UIBarbutton
注意:UIBarbutton有l(wèi)eftBarButton,rightBarButton和backBarButton,其中backBarButton由于帶有箭頭,需要單獨(dú)設(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: [UIFont systemFontOfSize:18],
UITextAttributeTextColor: [UIColor blueColor],
UITextAttributeTextShadowColor:[UIColor whiteColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};
[appearance setTitleTextAttributes:textAttributesforState:1];//forState為0時(shí)為下正常狀態(tài),為1時(shí)為點(diǎn)擊狀態(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需要單獨(dú)設(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];

愛(ài)華網(wǎng)本文地址 » http://www.klfzs.com/a/25101010/21320.html
愛(ài)華網(wǎng)



