iOS 自定义各类bar的属性

  在iOS应用开发中,经常需要为导航栏和标签栏设置相同的主题,一个一个去设置的话,就太麻烦了,可以通过对应用中所有的导航栏和标签栏同意设置背景、字体等属性。

  如:创建一个继承自“UINavigationController”的公共父类,然后应用中所有的NavigationController都继承UINavigationController,通过在UINavigationController类中的类方法

initialize中对导航栏属性进行设置,就会对项目中所有的导航栏控制器起作用

  示例代码如下:

 + (void)initialize
{ #warning 可以通过设置UITabBar主题的方式来修改UITabBar中按钮的颜色
UITabBar *tabBar = [UITabBar appearance];
NSMutableDictionary *tabAttrs = [NSMutableDictionary dictionary];
tabAttrs[UITextAttributeTextColor] = [UIColor orangeColor];
[tabBar setTintColor:[UIColor orangeColor]]; // 1 设置UINavigationBar
UINavigationBar *navBar = [UINavigationBar appearance]; //1.1 设置状态栏
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque; //1.2 设置背景图片
[navBar setBackgroundImage:[UIImage imageWithName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault]; //1.3.设置字体
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[UITextAttributeTextColor] = [UIColor blackColor];
attrs[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:CGSizeMake(, )];
attrs[UITextAttributeFont] = [UIFont systemFontOfSize:]; [navBar setTitleTextAttributes:attrs]; //2 设置导航条按钮主题
UIBarButtonItem *barItem = [UIBarButtonItem appearance];
//2.1 设置背景图
[barItem setBackButtonBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[barItem setBackButtonBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background_pushed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[barItem setBackButtonBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background_disable"] forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
//2.2 设置字体属性
NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionary]; itemAttrs[UITextAttributeTextColor] = iOS7 ? [UIColor orangeColor] : [UIColor blackColor];
itemAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:CGSizeMake(, )];
itemAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:];
[barItem setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
}
上一篇:ASP.NET控件GridView的使用& Xml操作注意事项


下一篇:(4.19)sql server中的事务模式(隐式事务,显式事务,自动提交事务)