【我就看看不说话】UIAppearance

来源:互联网 发布:专业美工电脑配置 编辑:程序博客网 时间:2024/04/26 04:17

iOS5提供了一个比较强大的工具UIAppearance,可以轻松的统一你的界面,它提供如下两个方法:

+ (id)appearance

+ (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...

第一个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样写:[[UINavigationBar appearance] setTintColor:myColor];
第二个方法是当出现在某个类的出现时候才会改变:例如:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];


       另外其它的UI外观修改如下:


       首先定义两个值:

[csharp] view plaincopy
  1. //这样方便下面多个UI界面设置,textAttributes:字体  
  2. id appearance;  
  3. NSDictionary *textAttributes = nil;  
1.导航条

代码如下:

[csharp] view plaincopy
  1. //导航条  
  2. {  
  3.     appearance = [UINavigationBar appearance];  
  4.     UIImage *navBackgroundImg =[UIImage imageNamed:@"background_nav"];  
  5.       
  6.     [appearance setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];  
  7. }  

2.标签栏(UITabbar)


代码如下:

[csharp] view plaincopy
  1. //标签栏  
  2. {  
  3.     appearance = [UITabBar appearance];  
  4.     UIImage *tabBarBackGroungImg =[UIImage imageNamed:@"tabbar_background"];  
  5.     [appearance setBackgroundImage:tabBarBackGroungImg];  
  6.       
  7.     UIImage * selectionIndicatorImage =[[UIImage imageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4, 0, 0, 0)] ;  
  8.     [appearance setSelectionIndicatorImage:selectionIndicatorImage];  
  9. }  
3.分段控件(UISegmentControl)

代码如下:

[csharp] view plaincopy
  1. //Segmente未选中背景  
  2. {  
  3.     //cap insets用来指定哪些区域是固定不变的,未制定的区域则会repeat  
  4.     UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];  
  5.       
  6.     UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];  
  7.       
  8.     UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ;  
  9.       
  10.     UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ;  
  11.       
  12.     UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"];  
  13.       
  14.       
  15.     appearance = [UISegmentedControl appearance];  
  16.       
  17.     [appearance setBackgroundImage:segmentUnselected  
  18.                           forState:stateNormal  
  19.                         barMetrics:UIBarMetricsDefault];  
  20.       
  21.     //Segmente选中背景  
  22.     [appearance setBackgroundImage:segmentSelected  
  23.                           forState:stateSelected  
  24.                         barMetrics:UIBarMetricsDefault];  
  25.       
  26.     //Segmente左右都未选中时的分割线  
  27.     //BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)  
  28.       
  29.     [appearance setDividerImage:segmentUnselectedUnselected  
  30.             forLeftSegmentState:stateNormal  
  31.               rightSegmentState:stateNormal  
  32.                      barMetrics:UIBarMetricsDefault];  
  33.       
  34.     [appearance setDividerImage:segmentSelectedUnselected  
  35.             forLeftSegmentState:stateSelected  
  36.               rightSegmentState:stateNormal  
  37.                      barMetrics:UIBarMetricsDefault];  
  38.       
  39.     [appearance setDividerImage:segUnselectedSelected  
  40.             forLeftSegmentState:stateNormal  
  41.               rightSegmentState:stateSelected  
  42.                      barMetrics:UIBarMetricsDefault];  
  43.       
  44.     //字体  
  45.     textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
  46.                       BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor,  
  47.                       BAR_BUTTON_TITLE_FONT,UITextAttributeFont,  
  48.                       BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor,  
  49.                       [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,  
  50.                       nil];  
  51.       
  52.     [appearance setTitleTextAttributes:textAttributes forState:1];  
  53.       
  54.     textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
  55.                       BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor,  
  56.                       BAR_BUTTON_TITLE_FONT,UITextAttributeFont,  
  57.                       BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor,  
  58.                       [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,  
  59.                       nil];  
  60.       
  61.     [appearance setTitleTextAttributes:textAttributes forState:0];  
  62. }  

4.UIBarbutton


注意:UIBarbutton有leftBarButton,rightBarButton和backBarButton,其中backBarButton由于带有箭头,需要单独设置。

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

代码如下:

[csharp] view plaincopy
  1. //UIBarButtonItem  
  2. {  
  3.     //只是修改导航条上的UIBarButtonItem  
  4.     appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];  
  5.     //backBarButton和leftBarButton,rightBarButton的字体同时设置  
  6.     textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
  7.                       BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor,  
  8.                       BAR_BUTTON_TITLE_FONT,UITextAttributeFont,  
  9.                       BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor,  
  10.                       [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,  
  11.                       nil];  
  12.       
  13.     [appearance setTitleTextAttributes:textAttributes forState:0];  
  14.       
  15.     textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
  16.                       BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor,  
  17.                       BAR_BUTTON_TITLE_FONT,UITextAttributeFont,  
  18.                       BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor,  
  19.                       [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,  
  20.                       nil];  
  21.       
  22.     [appearance setTitleTextAttributes:textAttributes forState:1];  
  23.       
  24.     UIImage *leftButton = [[UIImage imageNamed:@"bgLeftButton.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];  
  25.       
  26.     UIImage *normalButton = [[UIImage imageNamed:@"bgNormalButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];  
  27.       
  28.     //leftBarButton,rightBarButton背景  
  29.     [appearance setBackgroundImage:normalButton  
  30.                           forState:UIControlStateNormal  
  31.                              style:UIBarButtonItemStyleBordered  
  32.                         barMetrics:UIBarMetricsDefault];  
  33.       
  34.     [appearance setBackgroundImage:normalButton  
  35.                           forState:UIControlStateHighlighted  
  36.                              style:UIBarButtonItemStyleBordered  
  37.                         barMetrics:UIBarMetricsDefault];  
  38.       
  39.     //单独设置backBarButton背景  
  40.     [appearance setBackButtonBackgroundImage:leftButton  
  41.                                     forState:0  
  42.                                   barMetrics:UIBarMetricsDefault];  
  43.       
  44.     [appearance setBackButtonBackgroundImage:leftButton  
  45.                                     forState:1  
  46.                                   barMetrics:UIBarMetricsDefault];  
  47.       
  48.     [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1)  
  49.                                        forBarMetrics:UIBarMetricsDefault];  
  50.       
  51. }  

5.工具栏(UIToolbar)


代码如下:

[csharp] view plaincopy
  1. //toolBar  
  2. {  
  3.     appearance = [UIToolbar appearance];  
  4.     //样式和背景二选一即可,看需求了  
  5.     //样式(黑色半透明,不透明等)设置  
  6.     [appearance setBarStyle:UIBarStyleBlackTranslucent];  
  7.     //背景设置  
  8.     [appearance setBackgroundImage:[UIImage imageNamed:@"background_nav.png"]  
  9.                 forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];  
  10. }  



补充一个需要注意的地方:全局的设置最好在所有界面初始化前开始设置,否则可能失效。

一般写在appDelegate.m文件中。


原文连接:http://blog.csdn.net/shenjx1225/article/details/8552449

0 0