UITabBarController常見問題以及解決方案

常見問題


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;//默認狀態(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>
有錯的地方希望大家批評指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容