常見問題
1.設置UITabBarItem
2.items在選中狀態下的圖片、字體的顏色和大小
3.自定義TabBar
1.設置UITabBarItem
首先,羅列出UITabBarController中常用到的類:UITabBarController #UITabBarItem #UITabBar #UIBarItem,其中UIBarItem是最容易被忽略的。
UIBarItem是UITabBarItem的父類,<code>NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem </code>上面是官方開發文檔的內容,有時候我們在找屬性或者方法遇到困難的時候,不妨去父類或者相關協議里面去看看,可能會有意想不到的內容。如下:
<code>@property(nullable, nonatomic,copy) NSString*****title;//標題
@property(nullable, nonatomic,strong)UIImage***image;//默認狀態下的圖片
@property(nonatomic) UIEdgeInsets imageInsets;//設置圖片的大小</code>
設置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];//取消系統在選中狀態下多圖片的渲染
childController.tabBarItem.title = title;
YMNavgationController*NC = [[YMNavgationController alloc]initWithRootViewController:childController];
[self addChildViewController:NC];
}
</code>
然后就可以調用此方法設置相關的信息:
<code>-(void)addHomePage{
YMViewController*VC = [[YMViewController alloc]init];
[self addChildViewController:VC
imageName:@"tabBar_essence_icon"
selectedImageName:@"tabBar_essence_click_icon"
title:@"首頁"];
}
2.items在選中狀態下的圖片、字體的顏色和大小
前面已經解決了圖片去除系統渲染問題
<code> childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消系統在選中狀態下多圖片的渲</code>
調用系統的<code>@property(nonatomic) UIEdgeInsets imageInsets;//設置圖片的大小</code>可以改變圖片的大小,這個其實是調整圖片的內邊距。
圖片問題解決了,下面就是文字問題。解決文字問題就要用到UIBarItem這個類,系統給我們提供了一個方法<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;用處很大,這是為一次性解決問題體統的宏;以后大家遇到類似的宏可以試試;
//設置是否選中的文字顏色和大小
+(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方法,主要是解決自定義的按鈕問題,如果系統提供的方法能解決問題,我們也不會去自定義,主要是解決新浪微博等中間+按鈕問題
<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的分類,然后重寫了相關的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;//當遇到自定義按鈕時就跳過,自定義按鈕已經做好布局了
UIView*view = self.subviews[i];
NSInteger index = i<3?(i-1):i;//有5個按鈕,中間是自定義的按鈕,具體情況根據按鈕的數量而定
view.frame = CGRectMake(index*width, Y, width, height);
}
}
</code>
有錯的地方希望大家批評指正