iOS5及其以后提供了一個比較強大的工具UIAppearance,我們通過UIAppearance設置一些UI的全局效果峭火,這樣就可以很方便的實現(xiàn)UI的自定義效果又能最簡單的實現(xiàn)統(tǒng)一界面風格,它提供如下兩個方法。
+ (id)appearance
這個方法是統(tǒng)一全部改,比如你設置UINavBar的tintColor层皱,你可以這樣寫:[[UINavigationBarappearance] setTintColor:myColor];
+ (id)appearanceWhenContainedIn:(Class<>)ContainerClass,...
這個方法可設置某個類的改變:例如:設置UIBarButtonItem在UINavigationBar、UIPopoverController京痢、UITabbar中的效果奶甘。就可以這樣寫
[[UIBarButtonItemappearanceWhenContainedIn:[UINavigationBar class],[UIPopoverController class],[UITabbar class] nil]setTintColor:myPopoverNavBarColor];
warning:請注意*使用appearance設置UI效果最好采用全局的設置,在所有界面初始化前開始設置祭椰,否則可能失效臭家。
修改導航欄背景:
UINavigationBar * appearance = [UINavigationBarappearance];
UIImage *navBackgroundImg =[UIImage imageNamed:@"navBg.png”];
[appearancesetBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
標簽欄(UITabBar)
UITabBar *appearance = [UITabBar appearance];
//設置背景圖片
[appearancesetBackgroundImage:[UIImageimageNamed:@"tabbar_bg.png"]];
//門置選擇item的背景圖片
UIImage *selectionIndicatorImage =[[UIImage imageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4,0, 0,0)] ;
[appearancesetSelectionIndicatorImage:selectionIndicatorImage];
分段控件
UISegmentedControl *appearance = [UISegmentedControlappearance];
//Segmenteg正常背景
[appearancesetBackgroundImage:[UIImageimageNamed:@"Segmente.png"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
//Segmente選中背景
[appearancesetBackgroundImage:[UIImageimageNamed:@"Segmente_a.png"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
//Segmente左右都未選中時的分割線
//BarMetrics表示navigationbar的狀態(tài)疲陕,UIBarMetricsDefault表示portrait狀態(tài)(44pixelheight),UIBarMetricsLandscapePhone表示landscape狀態(tài)(32pixelheight)
[appearancesetDividerImage:[UIImage imageNamed:@"Segmente_line.png"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearancesetDividerImage:[UIImage imageNamed:@"Segmente_line.png"]
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearancesetDividerImage:[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)]};
[appearancesetTitleTextAttributes:textAttributes1forState:1];
NSDictionary *textAttributes2 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor:[UIColor whiteColor],
UITextAttributeTextShadowColor:[UIColor blackColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};
[appearancesetTitleTextAttributes:textAttributes2forState:0];
UIBarButton
注意:UIBarbutton有l(wèi)eftBarButton钉赁,rightBarButton和backBarButton蹄殃,其中backBarButton由于帶有箭頭,需要單獨設置你踩。
barButton背景設置是ios6.0及以后的诅岩,而backbutton是ios5.0及以后的,這里要注意带膜!
代碼如下:
//修改導航條上的UIBarButtonItem
UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBarclass], nil];
//設置導航欄的字體包括backBarButton和leftBarButton吩谦,rightBarButton的字體
NSDictionary *textAttributes = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor:[UIColor blueColor],
UITextAttributeTextShadowColor:[UIColor whiteColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};
[appearancesetTitleTextAttributes:textAttributesforState:1];//forState為0時為下正常狀態(tài),為1時為點擊狀態(tài)膝藕。
//修改leftBarButton式廷,rightBarButton背景效果
[appearancesetBackgroundImage:[UIImageimageNamed:@"navBarButton.png"]
forState:UIControlStateNormal
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
[appearancesetBackgroundImage:[UIImageimageNamed:@"navBarButton_a.png"]
forState:UIControlStateHighlighted
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
//backBarButton需要單獨設置背景效果。只能在ios6.0以后才能用
[appearancesetBackButtonBackgroundImage:[UIImage imageNamed:@"nav_bg.png"]
forState:0
barMetrics:UIBarMetricsDefault];
[appearancesetBackButtonBackgroundImage:[UIImage imageNamed:@"work.png"]
forState:1
barMetrics:UIBarMetricsDefault];
[appearancesetBackButtonTitlePositionAdjustment:UIOffsetMake(2,-1)
forBarMetrics:UIBarMetricsDefault];
工具欄
UIToolbar *appearance = [UIToolbar appearance];
//樣式和背景二選一即可芭挽,看需求了
//樣式(黑色半透明滑废,不透明等)設置
[appearancesetBarStyle:UIBarStyleBlackTranslucent];
//背景設置
[appearancesetBackgroundImage:[UIImageimageNamed:@"toolbarBg.png"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
UIAppearance實現(xiàn)原理
在通過UIAppearance調(diào)用“UI_APPEARANCE_SELECTOR”標記的方法來配置外觀時,UIAppearance實際上沒有進行任何實際調(diào)用袜爪,而是把這個調(diào)用保存起來(在Objc中可以用NSInvocation對象來保存一個調(diào)用)蠕趁。當實際的對象顯示之前(添加到窗口上,drawRect:之前)辛馆,就會對這個對象調(diào)用之前保存的調(diào)用俺陋。當這個setter調(diào)用后,你的界面風格自定義就完成了怀各。