?iOS5及其以后提供了一個比較強大的工具UIAppearance,可以輕松的統一你的界面,它提供如下兩個方法:
+?(id)appearance
+?(id)appearanceWhenContainedIn:(Class <>)ContainerClass,...
第一個方法是統一全部改,比如你設置UINavBar的tintColor,你可以這樣寫:[[UINavigationBar appearance] setTintColor:myColor];
第二個方法是當出現在某個類的出現時候才會改變:例如:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];
?另外其它的UI外觀修改如下:
?
? ? ? ?首先定義兩個值:
//這樣方便下面多個UI界面設置,textAttributes:字體 id appearance; NSDictionary *textAttributes = nil;
1.導航條
代碼如下:
//導航條 { appearance = [UINavigationBar appearance]; UIImage *navBackgroundImg =[UIImage imageNamed:@"background_nav"]; [appearance setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault]; }
?
2.標簽欄(UITabbar)
?
代碼如下:
//標簽欄 { appearance = [UITabBar appearance]; UIImage *tabBarBackGroungImg =[UIImage imageNamed:@"tabbar_background"]; [appearance setBackgroundImage:tabBarBackGroungImg]; UIImage * selectionIndicatorImage =[[UIImage imageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4, 0, 0, 0)] ; [appearance setSelectionIndicatorImage:selectionIndicatorImage]; }
3.分段控件(UISegmentControl)
代碼如下:
?
//Segmente未選中背景 { //cap insets用來指定哪些區域是固定不變的,未制定的區域則會repeat UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ; UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ; UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"]; appearance = [UISegmentedControl appearance]; [appearance setBackgroundImage:segmentUnselected forState:stateNormal barMetrics:UIBarMetricsDefault]; //Segmente選中背景 [appearance setBackgroundImage:segmentSelected forState:stateSelected barMetrics:UIBarMetricsDefault]; //Segmente左右都未選中時的分割線 //BarMetrics表示navigation bar的狀態,UIBarMetricsDefault 表示portrait狀態(44pixel height),UIBarMetricsLandscapePhone 表示landscape狀態(32pixel height) [appearance setDividerImage:segmentUnselectedUnselected forLeftSegmentState:stateNormal rightSegmentState:stateNormal barMetrics:UIBarMetricsDefault]; [appearance setDividerImage:segmentSelectedUnselected forLeftSegmentState:stateSelected rightSegmentState:stateNormal barMetrics:UIBarMetricsDefault]; [appearance setDividerImage:segUnselectedSelected forLeftSegmentState:stateNormal rightSegmentState:stateSelected barMetrics:UIBarMetricsDefault]; //字體 textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor, BAR_BUTTON_TITLE_FONT,UITextAttributeFont, BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset, nil]; [appearance setTitleTextAttributes:textAttributes forState:1]; textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor, BAR_BUTTON_TITLE_FONT,UITextAttributeFont, BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset, nil]; [appearance setTitleTextAttributes:textAttributes 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的字體同時設置 textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor, BAR_BUTTON_TITLE_FONT,UITextAttributeFont, BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset, nil]; [appearance setTitleTextAttributes:textAttributes forState:0]; textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor, BAR_BUTTON_TITLE_FONT,UITextAttributeFont, BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset, nil]; [appearance setTitleTextAttributes:textAttributes forState:1]; UIImage *leftButton = [[UIImage imageNamed:@"bgLeftButton.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]; UIImage *normalButton = [[UIImage imageNamed:@"bgNormalButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; //leftBarButton,rightBarButton背景 [appearance setBackgroundImage:normalButton forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault]; [appearance setBackgroundImage:normalButton forState:UIControlStateHighlighted style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault]; //單獨設置backBarButton背景 [appearance setBackButtonBackgroundImage:leftButton forState:0 barMetrics:UIBarMetricsDefault]; [appearance setBackButtonBackgroundImage:leftButton forState:1 barMetrics:UIBarMetricsDefault]; [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1) forBarMetrics:UIBarMetricsDefault]; }
5.工具欄(UIToolbar)
?
代碼如下:
?
//toolBar { appearance = [UIToolbar appearance]; //樣式和背景二選一即可,看需求了 //樣式(黑色半透明,不透明等)設置 [appearance setBarStyle:UIBarStyleBlackTranslucent]; //背景設置 [appearance setBackgroundImage:[UIImage imageNamed:@"background_nav.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; }
?
?
?
?
?
?
?
?
?
?