常見問題
1.設(shè)置UITabBarItem
2.items在選中狀態(tài)下的圖片校摩、字體的顏色和大小
3.自定義TabBar
1.設(shè)置UITabBarItem
首先埃撵,羅列出UITabBarController中常用到的類:UITabBarController #UITabBarItem #UITabBar #UIBarItem涡驮,其中UIBarItem是最容易被忽略的乳讥。
UIBarItem是UITabBarItem的父類,<code>NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem </code>上面是官方開發(fā)文檔的內(nèi)容耸携,有時候我們在找屬性或者方法遇到困難的時候昭殉,不妨去父類或者相關(guān)協(xié)議里面去看看苞七,可能會有意想不到的內(nèi)容。如下:
<code>@property(nullable, nonatomic,copy) NSString*****title;//標(biāo)題
@property(nullable, nonatomic,strong)UIImage***image;//默認(rèn)狀態(tài)下的圖片
@property(nonatomic) UIEdgeInsets imageInsets;//設(shè)置圖片的大小</code>
設(shè)置UITabBarItem一般的情況下我們都會自定義一個方法:
<code>
-(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];//取消系統(tǒng)在選中狀態(tài)下多圖片的渲染
childController.tabBarItem.title = title;
YMNavgationController*NC = [[YMNavgationController alloc]initWithRootViewController:childController];
[self addChildViewController:NC];
}
</code>
然后就可以調(diào)用此方法設(shè)置相關(guān)的信息:
<code>-(void)addHomePage{
YMViewController*VC = [[YMViewController alloc]init];
[self addChildViewController:VC
imageName:@"tabBar_essence_icon"
selectedImageName:@"tabBar_essence_click_icon"
title:@"首頁"];
}
2.items在選中狀態(tài)下的圖片挪丢、字體的顏色和大小
前面已經(jīng)解決了圖片去除系統(tǒng)渲染問題
<code> childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消系統(tǒng)在選中狀態(tài)下多圖片的渲</code>
調(diào)用系統(tǒng)的<code>@property(nonatomic) UIEdgeInsets imageInsets;//設(shè)置圖片的大小</code>可以改變圖片的大小,這個其實是調(diào)整圖片的內(nèi)邊距卢厂。
圖片問題解決了乾蓬,下面就是文字問題。解決文字問題就要用到UIBarItem這個類慎恒,系統(tǒng)給我們提供了一個方法<code>- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
其中的NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;用處很大任内,這是為一次性解決問題體統(tǒng)的宏;以后大家遇到類似的宏可以試試融柬;
//設(shè)置是否選中的文字顏色和大小
+(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];
}
大家可以考慮下把這種設(shè)置寫在+(void)initialize方法下的好處死嗦,當(dāng)然寫在其他地方也不會錯;
3.自定義TabBar
這個問題其實很簡單粒氧,大家在網(wǎng)上很容易找到資源越除,下面我就把重要的方法寫下來
新建一個類,繼承UITabBar
重寫-(instancetype)initWithFrame:(CGRect)frame方法外盯,主要是解決自定義的按鈕問題摘盆,如果系統(tǒng)提供的方法能解決問題,我們也不會去自定義饱苟,主要是解決新浪微博等中間+按鈕問題
<code>-(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;
}</code>
重寫-(void)layoutSubviews方法孩擂,這個主要是多按鈕進行布局
<code>-(void)layoutSubviews{
CGFloat width = self.width/5;
CGFloat height = self.height;
CGFloat Y = 0;
//布局加號按鈕
self.addButton.width = width;//類似的寫法是重寫了UIView的分類,然后重寫了相關(guān)的set 和get方法
self.addButton.height = height;
self.addButton.centerX = self.centerX;
//布局其他按鈕
for (NSInteger i = 0; i<self.subviews.count ;i++) {
if(![self.subviews[i]
isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;//當(dāng)遇到自定義按鈕時就跳過箱熬,自定義按鈕已經(jīng)做好布局了
UIView*view = self.subviews[i];
NSInteger index = i<3?(i-1):i;//有5個按鈕类垦,中間是自定義的按鈕,具體情況根據(jù)按鈕的數(shù)量而定
view.frame = CGRectMake(index*width, Y, width, height);
}
}
</code>
有錯的地方希望大家批評指正