【iOS开发】---- 强大的UI修改工具 UIAppearance

              iOS5及其以后提供了一个比较强大的工具UIAppearance,可以轻松的统一你的界面,它提供如下两个方法:

+ (id)appearance

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

第一个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样写:[[UINavigationBar appearance] setTintColor:myColor];
第二个方法是当出现在某个类的出现时候才会改变:例如:

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


       另外其它的UI外观修改如下:


       首先定义两个值:

    //这样方便下面多个UI界面设置,textAttributes:字体
    id appearance;
    NSDictionary *textAttributes = nil;
1.导航条

代码如下:

    //导航条
    {
        appearance = [UINavigationBar appearance];
        UIImage *navBackgroundImg =[UIImage imageNamed:@"background_nav"];
        
        [appearance setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
    }

2.标签栏(UITabbar)


代码如下:

    //标签栏
    {
        appearance = [UITabBar appearance];
        UIImage *tabBarBackGroungImg =[UIImage imageNamed:@"tabbar_background"];
        [appearance setBackgroundImage:tabBarBackGroungImg];
        
        UIImage * selectionIndicatorImage =[[UIImage imageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4, 0, 0, 0)] ;
        [appearance setSelectionIndicatorImage:selectionIndicatorImage];
    }
3.分段控件(UISegmentControl)

代码如下:

    //Segmente未选中背景
    {
        //cap insets用来指定哪些区域是固定不变的,未制定的区域则会repeat
        UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        
        UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        
        UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ;
        
        UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ;
        
        UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"];
        
        
        appearance = [UISegmentedControl appearance];
        
        [appearance setBackgroundImage:segmentUnselected
                              forState:stateNormal
                            barMetrics:UIBarMetricsDefault];
        
        //Segmente选中背景
        [appearance setBackgroundImage:segmentSelected
                              forState:stateSelected
                            barMetrics:UIBarMetricsDefault];
        
        //Segmente左右都未选中时的分割线
        //BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)
        
        [appearance setDividerImage:segmentUnselectedUnselected
                forLeftSegmentState:stateNormal
                  rightSegmentState:stateNormal
                         barMetrics:UIBarMetricsDefault];
        
        [appearance setDividerImage:segmentSelectedUnselected
                forLeftSegmentState:stateSelected
                  rightSegmentState:stateNormal
                         barMetrics:UIBarMetricsDefault];
        
        [appearance setDividerImage:segUnselectedSelected
                forLeftSegmentState:stateNormal
                  rightSegmentState:stateSelected
                         barMetrics:UIBarMetricsDefault];
        
        //字体
        textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                          BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor,
                          BAR_BUTTON_TITLE_FONT,UITextAttributeFont,
                          BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor,
                          [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,
                          nil];
        
        [appearance setTitleTextAttributes:textAttributes forState:1];
        
        textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                          BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor,
                          BAR_BUTTON_TITLE_FONT,UITextAttributeFont,
                          BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor,
                          [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,
                          nil];
        
        [appearance setTitleTextAttributes:textAttributes 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的字体同时设置
        textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                          BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor,
                          BAR_BUTTON_TITLE_FONT,UITextAttributeFont,
                          BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor,
                          [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,
                          nil];
        
        [appearance setTitleTextAttributes:textAttributes forState:0];
        
        textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                          BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextColor,
                          BAR_BUTTON_TITLE_FONT,UITextAttributeFont,
                          BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextShadowColor,
                          [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,
                          nil];
        
        [appearance setTitleTextAttributes:textAttributes forState:1];
        
        UIImage *leftButton = [[UIImage imageNamed:@"bgLeftButton.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
        
        UIImage *normalButton = [[UIImage imageNamed:@"bgNormalButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        
        //leftBarButton,rightBarButton背景
        [appearance setBackgroundImage:normalButton
                              forState:UIControlStateNormal
                                 style:UIBarButtonItemStyleBordered
                            barMetrics:UIBarMetricsDefault];
        
        [appearance setBackgroundImage:normalButton
                              forState:UIControlStateHighlighted
                                 style:UIBarButtonItemStyleBordered
                            barMetrics:UIBarMetricsDefault];
        
        //单独设置backBarButton背景
        [appearance setBackButtonBackgroundImage:leftButton
                                        forState:0
                                      barMetrics:UIBarMetricsDefault];
        
        [appearance setBackButtonBackgroundImage:leftButton
                                        forState:1
                                      barMetrics:UIBarMetricsDefault];
        
        [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1)
                                           forBarMetrics:UIBarMetricsDefault];
        
    }

5.工具栏(UIToolbar)


代码如下:

    //toolBar
    {
        appearance = [UIToolbar appearance];
        //样式和背景二选一即可,看需求了
        //样式(黑色半透明,不透明等)设置
        [appearance setBarStyle:UIBarStyleBlackTranslucent];
        //背景设置
        [appearance setBackgroundImage:[UIImage imageNamed:@"background_nav.png"]
                    forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    }



补充一个需要注意的地方:全局的设置最好在所有界面初始化前开始设置,否则可能失效。

机多图,比较漂亮。800*480的。其他尺寸自己改SysInfo.ini文件 X,Y是坐标,IconPushed为图标的地址,SizePushed,SizeNormal图标的大小。 GLOBALSETTINGS ShowMessage = 0 Quitno X = 0 x方向的位置(后同) Y = 0 y方向的位置(后同) Width = 480 桌面背景图片宽度 Height = 272 桌面背景图片高度 Arrange = No 排列与否,和windows桌面排列一样 BackgroundBitmap = \SDMMC\app\PIC\background.bmp 显示桌面背景图片,可以修改成你想显示的图片名称及位置 StartUpAnimation = FlyUp Accelerated = yes TopMost = no #--------------------------------- DATE 显示日期的设置, x = 60 y = 3 Color = FFFFFF 字体颜色(后同) FrameColor = 000000 Size = 18 字体大小(后同) Weight = 700 CreateFont TIME 显示时间的设置, x = 155 y = 3 Color = FFFFFF FrameColor = 000000 Format = HH':'mm':'ss' 时间格式 Size = 18 Weight = 700 CreateFont Interval = 1000 刷新间隔,单位好像是毫秒 BATTERY 显示电池信息, x = 10 y = 5 Width = 38 Height = 16 ColorBattery = A0A0A0 ColorHigh = 00A000 ColorLow = A0A000 ColorCritical = A00000 Segments = 10 电池电量显示分块数 #--------------------------------- #--------------------------------- ICONXPBUTTON 自定义快捷方式,注意每节前后有#----作为分段 x = 420 程序图标的显示位置,X方向 y = 1 程序图标的显示位置,y方向 Command = \SDMMC\应用软件\Off.exe 应用程序位置名称 SizeNormal = 48 图标大小,不能小于图标文件的尺寸 SizePushed = 48 按下去后的图标大小 ScaleAlpha = 100 比例 Ic\SDMMC\图标库\off.ico 快捷方式的图标位置名称 ScaleAlpha = 100 比例 Ic\SDMMC\图标库\off.ico 按下去后的图标位置名称 Quitno 程序启动后是否自动退出,YES是的,no不关闭 TEXT 快捷方式的文字说明 x = 25 文字说明的显示位置,X方向 y = 150 文字说明的显示位置,Y方向 Text = 语音电子书 文字说明的内容 Color = FFFFFF 文字的颜色 FrameColor = 000000 背景框颜色 Size = 16 文字的大小 Weight = 700 CreateFont #--------------------------------- #--------------------------------- ICONXPBUTTON x = 5 y = -16 NewIni = ..\Navi\Navi.ini 运行下一个页面 SizeNormal = 48 SizePushed = 48 S
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值