iOS UIAppearance使用详解

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS UIAppearance使用详解


  

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

(id)appearance

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


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

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

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


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


 具体UI外观修改如下:


1.修改导航栏背景

代码如下:

   UINavigationBar * appearance = [UINavigationBarappearance];

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

    

   [appearance setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];


2.标签栏(UITabbar

代码如下:

    UITabBar *appearance = [UITabBar appearance];

   //设置背景图片

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

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

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

    [appearance setSelectionIndicatorImage:selectionIndicatorImage];


3.分段控件(UISegmentControl

代码如下:

   UISegmentedControl *appearance = [UISegmentedControlappearance];

    

   //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表示navigationbar的状态,UIBarMetricsDefault表示portrait状态(44pixelheight),UIBarMetricsLandscapePhone表示landscape状态(32pixelheight

    

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

    

    [appearance setTitleTextAttributes:textAttributes1forState:1];

    

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

                               UITextAttributeTextColor: [UIColor whiteColor],

                               UITextAttributeTextShadowColor:[UIColor blackColor],

                               UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};

    

    [appearance setTitleTextAttributes:textAttributes2forState: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: [UIColor blueColor],

                               UITextAttributeTextShadowColor:[UIColor whiteColor],

                               UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(1, 1)]};

    

    [appearance setTitleTextAttributes:textAttributesforState: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];


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 VESA DSC(Display Stream Compression)是由视频电子标准协会(Video Electronics Standards Association)推出的一种高效低延迟的图像数据压缩技术,主要用于缓解高分辨率、高刷新率显示系统中显示接口的数据传输压力。它在不降低画质的前提下,可显著降低带宽需求,从而提升显示系统性能。VESA DSC有多个版本,从 v1.1 到 v1.2b,每个版本都对前一版本进行了改进和优化,比如修复错误、提升性能或增强兼容性。其中,v1.1 是基础版本,v1.2 系列则是对原始规范的逐步完善,v1.2a 和 v1.2b 通常是针对特定问题或新需求的微调版本。 DSC 压缩规格说明书详细介绍了 DSC 的工作原理、编码流程、解码算法以及与显示系统的接口规范,涵盖以下关键点:1. 压缩算法:DSC 结合了熵编码和预测编码,通过预测连续像素间的差异来减少传输信息量,其中熵编码单元(EEU)和熵解码单元(EDU)负责数据的编码和解码。2. 帧内和帧间预测:DSC 支持基于同一帧内像素信息的帧内预测,以及利用前后帧像素关系的帧间预测,从而在保持画质的同时提高压缩效率。3. 带宽管理:DSC 标准可动态调整压缩比率,以适配显示设备的实际带宽需求,确保流畅显示。4. 实时性和低延迟:DSC 设计时注重实时应用,尽可能减少压缩和解压缩过程中的延迟,这对于游戏和专业图形应用至关重要。5. 兼容性和互操作性:作为 VESA 的标准,DSC 与其他显示接口标准(如 HDMI、DP 等)兼容,保证不同设备间的互操作性。6. C Model 源码:提供的 C 模型源码是 DSC 算法的参考实现,有助于开发者理解 DSC 工作机制,用于开发 DSC 兼容的硬件或软件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值