iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下两个方法。

+ (id)appearance

这个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样写:[[UINavigationBar appearance] setTintColor:myColor];

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

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

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

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

 具体UI外观修改如下:

1.修改导航栏背景

代码如下:

UINavigationBar * appearance = [UINavigationBar appearance];

UIImage *navBackgroundImg =[UIImage imageNamed:@"navBg.png”];

[appearance setBackgroundImage:navBackgroundImgforBarMetrics:UIBarMetricsDefault];

2.标签栏(UITabbar)

代码如下:

UITabBar *appearance = [UITabBar appearance];

//设置背景图片

[appearance setBackgroundImage:[UIImage imageNamed:@"tabbar_bg.png"]];

//门置选择item的背景图片

UIImage * selectionIndicatorImage =[[UIImageimageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4, 0, 0,0)] ;

[appearance setSelectionIndicatorImage:selectionIndicatorImage];

3.分段控件(UISegmentControl)

代码如下:

UISegmentedControl *appearance = [UISegmentedControl appearance];

//Segmenteg正常背景

[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente.png"]

forState:UIControlStateNormal

barMetrics:UIBarMetricsDefault];

//Segmente选中背景

[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente_a.png"]

forState:UIControlStateSelected

barMetrics:UIBarMetricsDefault];

//Segmente左右都未选中时的分割线

//BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)

[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: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};

[appearance setTitleTextAttributes:textAttributes1 forState:1];

NSDictionary *textAttributes2 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],

UITextAttributeTextColor: [UIColor whiteColor],

UITextAttributeTextShadowColor: [UIColor blackColor],

UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};

[appearance setTitleTextAttributes:textAttributes2 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 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],

UITextAttributeTextColor: [UIColorblueColor],

UITextAttributeTextShadowColor: [UIColorwhiteColor],

UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};

[appearance setTitleTextAttributes:textAttributes forState:1];//forState为0时为下正常状态,为1时为点击状态。

//修改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需要单独设置背景效果。只能在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];

//样式和背景二选一即可,看需求了

//样式(黑色半透明,不透明等)设置

[appearance setBarStyle:UIBarStyleBlackTranslucent];

//背景设置

[appearance setBackgroundImage:[UIImage imageNamed:@"toolbarBg.png"]

forToolbarPosition:UIToolbarPositionAny

barMetrics:UIBarMetricsDefault];

方法二:太阳方法

设置单个的页面的样式,在页面出来之前往当前页面写这一句

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar"] forBarMetrics:UIBarMetricsDefault];

UIAppearance使用详解-备的更多相关文章

  1. get_template_part() 函数详解备忘(转)

    最近研究官方主题 Twenty Eleven ,有一些东西网上现成的中文资料不好找,在博客里记载下来,算是分享,也算是备忘,wordpress 3.0 以后就开始便有了get_template_par ...

  2. [转]iOS UIAppearance使用详解

    在iOS 5以前,自定义原生控件的外观并没有原生支持,因此开发人员感觉很麻烦.开发人员经常面临的问题是修改一个控件所有实例的外观.解决这个问题的正确方法是重写一遍控件.但由于这么做非常费时,一些开发人 ...

  3. 转iOS UIAppearance使用详解

    iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下 ...

  4. java之yield(),sleep(),wait()区别详解-备忘笔记

    备注:转载地址,http://dylanxu.iteye.com/blog/1322066,谢谢作者 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但 ...

  5. java之yield(),sleep(),wait()区别详解-备忘笔记[转]

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  6. java之yield(),sleep(),wait()区别详解-备忘笔记(转)

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  7. Nginx 反向代理、负载均衡、页面缓存、URL重写、读写分离及简单双机热备详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx  (windows下nginx安装.配置与使用) 四.Nginx之反向代理 五.Nginx之负载均衡  (负载均衡算法:nginx负载算法 up ...

  8. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  9. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

随机推荐

  1. chord原理的解读

    chord: A Scalable Peer-to-peer Lookup Service for Internet Application 在 P2P 系统中,有效地定位分布在网络中不同节点的数据资 ...

  2. UltraEdit-32 恢复到初始默认配置

    不小心把UltraEdit-32搞乱了,字体不管怎么设置,都回不去了..然后那些菜单也被弄得乱七糟八的....于是就想把这些设置都搞成默认的.重装UltraEdit-32.清理注册表.结果发现都没有用 ...

  3. 减少leftJoin的使用 (转)

    作为开发,你是否经常碰到下面需要转换用户ID成用户名称的情况: 可惜你的这些业务表出于最少冗余设计要求,只有UserId,而没有UserName,这时你不得不破坏你一个类封装一个表的美好想法, 在你的 ...

  4. Vericant维立克 | 氪加

    Vericant维立克 | 氪加 Vericant维立克

  5. editplus配置详:

    1:设置删除整行快捷键 2:设置背景颜色 3:php 开发环境 在 http://download.csdn.net/detail/vspeter/6002287 下载 editplus 的php语法 ...

  6. spring简单入门示例

    1 控制反转IOC\依赖注入DI,因为翻译的不同,因此有两个名字.       控制反转意思就是说,当我们调用一个方法或者类时,不再有我们主动去创建这个类的对象,控制权交给别人(spring). 依赖 ...

  7. OC 图片圆角实现

    self.imageTouX.layer.masksToBounds=YES; self.imageTouX.layer.cornerRadius=/2.0f; //设置为图片宽度的一半出来为圆形 s ...

  8. MVC 校验

    校验保障了MVC 应用程序安全性. Models 文件夹包含表示应用程序模型的类 1,创建一个项目MvcValidateDemo. 2,创建一个实体类UserInfo在Models中,包含Id.Use ...

  9. C# 约瑟夫环算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. hdu 2853

    虚拟赛一开始lyf就对我说这是一道匹配的题目,我一看明显裸的最优匹配,敲完提交wrong, 题目要求改变尽量少的公司,就是如果遇到相等的权值,优先选择跟他原来匹配的,KM匹配是按序号大小来的,如果一个 ...