修改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的空间都可以使用上面的方法实现宽度和高度的重新设置,如果有必要的话。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值