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. hdu Fibonacci

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> u ...

  2. 深入理解Java的protected修饰符

    其实Java的protected修饰符,权限定义的很微妙,大致有以下几种: (1)protected控制符用于修饰方法和成员变量: (2)一个类的protected方法或成员变量,在包外是不能通过该类 ...

  3. cf492B Vanya and Lanterns

    B. Vanya and Lanterns time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. 算法导论(第三版)Exercises4.2(第四章二节)

    4.2-1(计算结果) 18  14 62  66 4.2-2(Strassen算法计算矩阵乘法) void multiplyMatrix(int a[], int b[], int n, int r ...

  5. 什么是 docker?

    关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...

  6. poj 2836 Rectangular Covering(状态压缩dp)

    Description n points are given on the Cartesian plane. Now you have to use some rectangles whose sid ...

  7. iOS 使用pods报错问题 pod --version

    错误信息如下 find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Us ...

  8. #python基础学习模块:marshal 对象的序列化

    #标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...

  9. html打开个人QQ聊天页面

    打开qq聊天页面(有权限需要添加好友) <a href="tencent://message/?uin=1578929883&Site=&Menu=yes" ...

  10. JSP简单练习-数组应用实例

    <%@ page contentType="text/html; charset=gb2312" %> <html> <body> <% ...