UITabBarController的 tabBar 高度动态调节 , 测试环境 iOS9.3.3
可以通过给UITabbar写一个拓展来实现改变UITabBarController的UITabBar的高度;
假如我需要设置UITabBarController的tabBar高度为60,只需要设置如:
@interface UITabBar (ext)
@end
@implementation UITabBar (ext)
-(CGSize)sizeThatFits:(CGSize)size
{
CGSize sizeThatFits = [super sizeThatFits:size];
sizeThatFits.height = 60;
return sizeThatFits;
}
如果我需要动态的设置UITabBarController的tabbar高度,如微信中改变字体大小,tabbar的高度也会随之变化,通过一个触发函数:
我们先定义一个继承自UITabBarController的子类
@interface MyTabBarController : UITabBarController
添加以下方法到 MyTabBarController中
- (void) setTabBarLocationWithLevel:(NSInteger) fontLevel
{
self.tabBar.tag = [PublicUtils getCurrentFontLevel];
[self.tabBar setNeedsLayout];
[self.tabBar layoutSubviews];
CGFloat h = fontLevel *2+47;
UITabBar *tabBar = self.tabBar;
CGRect frame = tabBar.frame;
tabBar.backgroundImage = nil;
frame.size.height = h;
frame.origin.y = self.view.frame.size.height - h;
tabBar.frame = frame;
}
这里利用tag值保存fontLevel,然后下面的拓展根据 tag 动态设置 高度
然后更改UITabBar的拓展为如下
@interface UITabBar (ext)
@end
@implementation UITabBar (ext)
-(CGSize)sizeThatFits:(CGSize)size
{
NSInteger fontLevel = self.tag;
CGFloat h = fontLevel *2+47;
CGSize sizeThatFits = [super sizeThatFits:size];
sizeThatFits.height = h;
return sizeThatFits;
}
@end
由于 UITabBar中有一层UITabBarBackgroundView并不会随着UITabBar的高度变化,而这一层是控制tabbar的颜色的,动态设置的时候,不会变化,这样看起来就不完美了,这时候可以采取向tabbar上添加一层UIView,用来控制颜色,具体如下:
UIView* tabbarback = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.tabBar.frame))];
tabbarback.backgroundColor = [UIColor redColor];
tabbarback.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.tabBar insertSubview:tabbarback atIndex:0];