iOS UIAppearance使用详解

本文详细介绍如何使用 iOS 的 UIAppearance API 来统一设置应用程序中 UI 控件的外观风格,包括导航栏、标签栏、分段控件、工具栏等,并提供具体的代码示例。

iOS UIAppearance使用详解

  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(11)]};

    

    [appearance setTitleTextAttributes:textAttributes1 forState:1];

    

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

                                     UITextAttributeTextColor: [UIColor whiteColor],

                                     UITextAttributeTextShadowColor: [UIColor blackColor],

                                     UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(11)]};

    

    [appearance setTitleTextAttributes:textAttributes2 forState:0];



4.UIBarbutton

注意UIBarbuttonleftBarButtonrightBarButtonbackBarButton,其中backBarButton由于带有箭头,需要单独设置。

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

代码如下:

    //修改导航条上的UIBarButtonItem

    UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];


    //设置导航栏的字体包括backBarButtonleftBarButtonrightBarButton的字体

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

                                     UITextAttributeTextColor: [UIColorblueColor],

                                     UITextAttributeTextShadowColor: [UIColorwhiteColor],

                                     UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(11)]};

    

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


    

    //修改leftBarButtonrightBarButton背景效果

    [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];

Ymodem协议是一种在串行通信中用于数据传输的机制,起源于早期的Xmodem协议。该协议具备处理大容量数据文件的能力,支持数据校验、文件属性传递以及多文件批量传输。Qt是一个面向C++的跨平台开发框架,适用于构建图形界面程序及后台服务类应用。当结合Qt实现Ymodem协议时,能够开发出适用于多种设备的通信程序,包括桌面系统、嵌入式设备和移动终端。 在Qt中实现Ymodem协议,需要掌握Qt的信号与槽机制、串口通信模块(如QSerialPort)以及线程管理技术。实现过程中通常包含以下主要环节: 1. 连接初始化:在通信双方确认状态后,启动数据传输流程。 2. 数据块传输:协议支持多种数据块大小,如128字节或1024字节。每个数据块需包含数据内容、编号及校验信息。接收端需对数据进行校验以保证完整性。 3. 序列管理与响应机制:通过编号确保数据顺序,发送端在收到确认信号后继续下一块传输。若未收到有效响应,则需重传。 4. 文件元信息传输:在正式传输前,发送方需传递文件名称及大小等信息,以便接收方进行存储准备。 5. 批量传输模式:支持多个文件的连续传输,需在数据中区分不同文件的块。 6. 传输结束机制:通常通过发送空数据块来终止通信过程。 在Qt中实现Ymodem协议的代码,通常需要创建多个类来封装串口通信逻辑及协议处理功能,同时需考虑异常处理和数据恢复策略。代码涉及对QSerialPort的配置、数据流的监控、事件循环的管理等。此外,为提升用户体验,开发人员可能在代码中加入进度显示、传输速率计算等功能。 Qt的跨平台特性使得基于其开发的Ymodem协议实现具备良好的可移植性,可在不同操作系统上运行。开发此类程序需要开发者对Ymodem协议有深入了解,并熟悉Qt框架的使用,包括信号机制、多线程及串口通信等技术。高质量的代码实现能够确保协议在不同运行环境下的稳定性与可靠性。 Ymodem协议的实现常出现在开源项目中,为开发者提供了参考和借鉴,有助于技术交流与社区发展。根据文件名“SerialPortYmodem”推测,该文件可能用于处理与串口通信相关的Ymodem逻辑。开发者在使用时应仔细查阅文档,理解各模块功能,以便正确集成和应用。 综上,Qt实现Ymodem协议的开发涉及对协议机制与Qt框架的深入理解,能够构建出满足多种串行通信需求的程序。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值