常见问题
1.设置UITabBarItem
2.items在选中状态下的图片、字体的颜色和大小
3.自定义TabBar
1.设置UITabBarItem
首先,罗列出UITabBarController中常用到的类:UITabBarController #UITabBarItem #UITabBar #UIBarItem,其中UIBarItem是最容易被忽略的。
UIBarItem是UITabBarItem的父类,NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem
上面是官方开发文档的内容,有时候我们在找属性或者方法遇到困难的时候,不妨去父类或者相关协议里面去看看,可能会有意想不到的内容。如下:
@property(nullable, nonatomic,copy) NSString*****title;//标题
@property(nullable, nonatomic,strong)UIImage***image;//默认状态下的图片
@property(nonatomic) UIEdgeInsets imageInsets;//设置图片的大小
设置UITabBarItem一般的情况下我们都会自定义一个方法:
-(void)addChildViewController:(UIViewController )childController imageName:(NSString)name selectedImageName:(NSString)selectedName title:(NSString)title{
childController.tabBarItem.image = [UIImage imageNamed:name];
childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消系统在选中状态下多图片的渲染
childController.tabBarItem.title = title;
YMNavgationController*NC = [[YMNavgationController alloc]initWithRootViewController:childController];
[self addChildViewController:NC];
}
然后就可以调用此方法设置相关的信息:
-(void)addHomePage{
YMViewController*VC = [[YMViewController alloc]init];
[self addChildViewController:VC
imageName:@"tabBar_essence_icon"
selectedImageName:@"tabBar_essence_click_icon"
title:@"首页"];
}
2.items在选中状态下的图片、字体的颜色和大小
前面已经解决了图片去除系统渲染问题
childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消系统在选中状态下多图片的渲
调用系统的@property(nonatomic) UIEdgeInsets imageInsets;//设置图片的大小
可以改变图片的大小,这个其实是调整图片的内边距。
图片问题解决了,下面就是文字问题。解决文字问题就要用到UIBarItem这个类,系统给我们提供了一个方法- (void)setTitleTextAttributes:(nullable NSDictionary
其中的NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;用处很大,这是为一次性解决问题体统的宏;以后大家遇到类似的宏可以试试;
//设置是否选中的文字颜色和大小
+(void)initialize{
NSMutableDictionary*attbs= [NSMutableDictionary dictionary];
attbs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attbs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary*selecteAttbs = [NSMutableDictionary dictionary];
selecteAttbs[NSFontAttributeName] = attbs[NSFontAttributeName];
selecteAttbs[NSForegroundColorAttributeName] =[UIColor darkGrayColor];
UITabBarItem*items = [UITabBarItem appearance];
[items setTitleTextAttributes:attbs forState:UIControlStateNormal];
[items setTitleTextAttributes:selecteAttbs forState:UIControlStateSelected];
}
大家可以考虑下把这种设置写在+(void)initialize方法下的好处,当然写在其他地方也不会错;
3.自定义TabBar
这个问题其实很简单,大家在网上很容易找到资源,下面我就把重要的方法写下来
新建一个类,继承UITabBar
重写-(instancetype)initWithFrame:(CGRect)frame方法,主要是解决自定义的按钮问题,如果系统提供的方法能解决问题,我们也不会去自定义,主要是解决新浪微博等中间+按钮问题
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
[self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
UIButton*addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[addButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[self addSubview:addButton];
self.addButton = addButton;
}
重写-(void)layoutSubviews方法,这个主要是多按钮进行布局
-(void)layoutSubviews{
CGFloat width = self.width/5;
CGFloat height = self.height;
CGFloat Y = 0;
//布局加号按钮
self.addButton.width = width;//类似的写法是重写了UIView的分类,然后重写了相关的set 和get方法
self.addButton.height = height;
self.addButton.centerX = self.centerX;
//布局其他按钮
for (NSInteger i = 0; i
}
有错的地方希望大家批评指正