修改UITabBar高度

背景

iOS系統框架UIKit提供了可供使用的tab視圖UITabBar,使用起來很方便,但是它是有一個默認高度的,在外部是不能隨便修改的。

如果我們想通過如下代碼設置tabBar的高度爲56的話,會發現設置不生效,無論高度寫多少,tabBar的高度總是不變,還是系統默認的高度。

self.tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-56, CGRectGetWidth(self.view.frame), 56)];

那麼怎麼修改系統默認的tabBar的高度呢?

修改系統默認UITabBar高度

1. 自己實現一個TabBar視圖

我們可以模仿系統的UITabBar的風格,樣式和API,自己定義一個TabBar。自定義的好處就是想怎麼改就怎麼改,怎麼玩都行。

自定義這裏就不說了,比較簡單,這裏重點說一下第二個辦法,複寫-sizeThatFits()方法。

2. 複寫-sizeThatFits()方法

UITabBar繼承於UIView,UIView有一個方法:

- (CGSize)sizeThatFits:(CGSize)size;     // return 'best' size to fit given size. does not actually resize view. Default is return existing view size

在view內複寫這個方法返回一個size來重設view的寬和高。一般的UIView我們也不用重寫該方法,因爲外部直接能設置其frame來設置view的寬和高,UITabBar有點特殊,它是系統給定了一個高度的。

創建一個類繼承UITabBar,直接在內部複寫-sizeThatFits()方法返回固定的寬和高,直接返回一個我們想要的高度。

// over wite this function to changed the tabbar height.
- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize fitsSize = [super sizeThatFits:size];
    fitsSize.height = 56;// give a height to tabbar
    return fitsSize;
}

上面的做法可以實現業務,但是同時代碼也被寫死了,外部也是不能通過設置frame來改變tabBar的高度,我們可以通過下面的方法來實現外部直接通過frame來設置tabBar的高度,像UIView一樣。

首先,我們創建一個全局變量originFrame來接外部傳進來的尺寸,
其次,複寫-sizeThatFits()方法來設置tabBar傳入的尺寸。

@interface ENTabBar ()

@property (nonatomic) CGRect originFrame;

@end

@implementation ENTabBar

// over wite this function to changed the tabbar height.
- (CGSize)sizeThatFits:(CGSize)size
{
//    CGSize fitsSize = [super sizeThatFits:size];
//    fitsSize.height = 56;// give a height to tabbar
//    return fitsSize;
    return self.originFrame.size;
}

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray <NSString *> *)titles
                   imageNames:(NSArray <NSString *> *)imageNames
{
    if (self = [super initWithFrame:frame])
    {
        self.originFrame = frame;
        
        NSMutableArray *items = [NSMutableArray array];
        for (int i = 0; i < titles.count; i++)
        {
            // set the image, origin image and do not disturbed by tint color.
            UIImage *image = [UIImage imageNamed:imageNames[i]];
            UIImage *correctImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:titles[i] image:correctImage tag:i+1000];
            
            // item text set
            [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
            [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
            
            [items addObject:item];
        }
        [self setItems:items];
    }
    return self;
}

- (NSUInteger)indexOfItem:(UITabBarItem *)item
{
    NSAssert(![self.items containsObject:item], @"unvalid item");
    return [self.items indexOfObject:item];
}

@end

這樣的話我們就可以直接像UIView一樣,在外部直接設置TabBar的高度了。

self.tabBar = [[ENTabBar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-56, CGRectGetWidth(self.view.frame), 56) titles:titles imageNames:imageNames];

擴展

繼承UIView的空間都可以使用上面的方法實現寬度和高度的重新設置,如果有必要的話。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章