iOS笔记之Appearance方法

iOS 5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,但是需要注意使用细则:
**
1、控件遵守了UIAppearance协议,才能对控件进行appearance设置
2、只有被UI_APPEARANCE_SELECTOR这个宏修饰的属性才能使用appearance进行设置,其他属性则不具备该功能
3、appearance设置需要在该控制显示之前设置完成,否则可能无效
**

它提供如下两个方法:

+ (id)appearance;

这个方法是统一全部改,比如你设置UINavigationBar的tintColor,你可以这样写:

[[UINavigationBar appearance] setTintColor:myColor];
+ (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...

这个方法可设置某个类的改变,例如:
设置UIBarButtonItem 在UINavigationBar、UIPopoverController、UITabbar中的效果,就可以这样写:

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

请注意:使用appearance设置UI效果最好采用全局的设置,在所有界面初始化前开始设置,否则可能失效。

具体UI外观修改如下:

1.修改导航栏背景
代码如下:

UINavigationBar * appearance = [UINavigationBar appearance];
UIImage *navBgImg =[UIImage imageNamed:@"bgImage"];
    
[appearance setBackgroundImage:navBgImg forBarMetrics:
  UIBarMetricsDefault];

2.标签栏(UITabbar)
代码如下:

UITabBar *appearance = [UITabBar appearance];
//设置背景图片
[appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"]];
//设置选择item的背景图片
UIImage * selectedImage =[[UIImage imageNamed:@"sliderImage"]
  resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2)] ;
[appearance setSelectionIndicatorImage:selectedImage];

3.分段控件(UISegmentControl)
代码如下:

UISegmentedControl *appearance = [UISegmentedControl appearance];
    
//Segmenteg正常背景
[appearance setBackgroundImage:[UIImage imageNamed:@"image"]
                      forState:UIControlStateNormal
                    barMetrics:UIBarMetricsDefault];
    
//Segmente选中背景
[appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"]
                     forState:UIControlStateSelected
                   barMetrics:UIBarMetricsDefault];
    
/*
Segmente左右都未选中时的分割线
BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示
portrait状态(44pixel height),UIBarMetricsLandscapePhone 
表示landscape状态(32pixel height
*/
    
[appearance setDividerImage:[UIImage imageNamed:@"line"]
        forLeftSegmentState:UIControlStateNormal
          rightSegmentState:UIControlStateNormal
                 barMetrics:UIBarMetricsDefault];
    
[appearance setDividerImage:[UIImage imageNamed:@"line"]
        forLeftSegmentState:UIControlStateSelected
          rightSegmentState:UIControlStateNormal
                 barMetrics:UIBarMetricsDefault];
    
[appearance setDividerImage:[UIImage imageNamed:@"line"]
        forLeftSegmentState:UIControlStateNormal
          rightSegmentState:UIControlStateSelected
                 barMetrics:UIBarMetricsDefault];
    
//字体
NSDictionary *textAtt1 = @{ 
NSFontAttributeName: [UIFont systemFontOfSize:18], 
NSForegroundColorAttributeName: [UIColor blueColor], 
NSShadowAttributeName: [UIColor whiteColor]};
    
[appearance setTitleTextAttributes:textAtt1 forState:1];
    
NSDictionary *textAtt2 = @{ 
NSFontAttributeName: [UIFont systemFontOfSize:18],
NSForegroundColorAttributeName: [UIColor blueColor], 
NSShadowAttributeName: [UIColor whiteColor]};
    
[appearance setTitleTextAttributes:textAtt2 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的字体
NSDictionary *textAttributes = @{ 
NSFontAttributeName: [UIFont systemFontOfSize:18], 
NSForegroundColorAttributeName: [UIColor blueColor], 
NSShadowAttributeName: [UIColor whiteColor]};
    
[appearance setTitleTextAttributes:textAttributes forState:1];
//forState为0时为下正常状态,为1时为点击状态。
  
//修改leftBarButton,rightBarButton背景效果
[appearance setBackgroundImage:[UIImage imageNamed:@"image"]
                      forState:UIControlStateNormal
                         style:UIBarButtonItemStyleBordered
                    barMetrics:UIBarMetricsDefault];
    
[appearance setBackgroundImage:[UIImage imageNamed:@"image"]
                      forState:UIControlStateHighlighted
                         style:UIBarButtonItemStyleBordered
                    barMetrics:UIBarMetricsDefault];
 
//backBarButton需要单独设置背景效果,只能在ios6.0以后才能用
[appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"bgImage"]
                                forState:0
                              barMetrics:UIBarMetricsDefault];
    
[appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"image"]
                                forState:1
                              barMetrics:UIBarMetricsDefault];
    
[appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1)
                                   forBarMetrics:UIBarMetricsDefault];

5.工具栏(UIToolbar)

UIToolbar *appearance = [UIToolbar appearance];
//样式和背景二选一即可,看需求了
//样式(黑色半透明,不透明等)设置
[appearance setBarStyle:UIBarStyleBlackTranslucent];
//背景设置
[appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"]
            forToolbarPosition:UIToolbarPositionAny
                    barMetrics:UIBarMetricsDefault];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,174评论 8 184
  • 你是我的一生挚爱,但我终究会跟另一个人走完一生
    应许93阅读 51评论 0 0
  • 01 有一天小明去逛街,在一家鞋店前看上一双皮鞋,问看店的伙计皮鞋多少米?伙计不知价格便向屋内的掌柜问道:“XY型...
    放慢脚步阅读 922评论 0 3
  • 希望 太爷已到耄耋之年 头发花白 牙齿稀疏有洞 但还是心存信念 背着我信心满满 一步步地攀登 石阶会印证一种顽强 ...
    笔耕心田阅读 128评论 0 1