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. android手机端保存xml数据

    1.前面写的这个不能继续插入数据,今天补上,当文件不存在的时候就创建,存在就直接往里面添加数据. 2.代码如下: <pre name="code" class="j ...

  2. PhoneGap and Titanium

    http://mobile.51cto.com/web-338270.htm http://www.udpwork.com/item/6117.html http://blog.cnbang.net/ ...

  3. RazorPad中的ModelProvider

    在RazorPad的右侧 我们可以提供模型的结构,Json数据结构体 当提供多个的时候 是Json中的数组 [{     Name: "NI" }, {     Name: &qu ...

  4. bzoj1786

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1786 刚看上去觉得挺吓人的...... 冥冥之中我的内心深处告诉我填进去的数一定是非严格递增的 ...

  5. 微软 Dynamics AX 学习步骤

    第一步:了解到AX的架构,AOT结构,了解AOT中表,窗体,类,job,菜单,菜单项的基础开发.知道代码可以写在那里,每个对象以及对象内部的具体设置.如果你不了解类,继承,这些,那么就需要找一下讲述类 ...

  6. Ext中窗体第二次点击报错或者其内控件不显示的问题,弄了2天才解决,记录下

    registerPanel.js: registerPanel = new Ext.form.FormPanel({ id:'registerPanel', layout:'form', autoHe ...

  7. 使用教程 - BestSync同步软件 - SQL2008R2 数据库定时备份解决方案

    需求: 1.      某公司的管理软件,数据库为SQL2008R2.2.      将整个数据库作为一个文件,定时同步到FTP 服务器3.      需要有多个备份,每同步一次,都备份上次的文件到备 ...

  8. Unity怎样在Editor下运行协程(coroutine)

    在处理Unity5新的AssetBundle的时候,我有一个需求,须要在Editor下(比方一个menuitem的处理函数中,游戏没有执行.也没有MonoBehaviour)载入AssetBundle ...

  9. Git diff (---和+++具体解释)

    如果两个文件相似度很高,那么上下文格式的diff,将显示大量重复的内容,很浪费空间.1990年,GNU diff率先推出了"合并格式"的diff,将f1和f2的上下文合并在一起显示 ...

  10. [转]iOS开发使用半透明模糊效果方法整理

    转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...