项目中遇到需要修改tabBar顶部横线的颜色,去网上找了好多 效果都不太理想 于是总结了一下 方便日后查阅
需要实现的效果是tabBar背景色为#FFFFFF 顶部横线颜色为#EDEDED 代码如下
[self changeLineOfTabbarColor]; //修改tabBar顶部横线颜色
[self changeTabbarColor]; //修改tabBar背景色
- (void)changeLineOfTabbarColor {
CGRect rect = CGRectMake(0.0f, 0.0f, kDeviceWidth, 0.5);
UIGraphicsBeginImageContextWithOptions(rect.size,NO, 0);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithHexString:@"#EDEDED"].CGColor);
CGContextFillRect(context, rect);
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.tabBarViewController.tabBar setShadowImage:image];
[self.tabBarViewController.tabBar setBackgroundImage:[UIImage new]];
}
- (void)changeTabbarColor {
CGRect frame;
UIView *tabBarView = [[UIView alloc] init];
tabBarView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
if (DEVICE_IS_IPHONE_X) {
frame = CGRectMake(0, 0, kDeviceWidth, 83);
}else {
frame = self.tabBarViewController.tabBar.bounds;
}
tabBarView.frame = frame;
[[UITabBar appearance] insertSubview:tabBarView atIndex:0];
}由于iPhone X的tabBar的高度为83 所以需要根据机型判断进行相应处理
#define DEVICE_IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
完成之后的效果如下:
去除tabBar顶部横线的方法
self.tabBarViewController.tabBar.clipsToBounds = YES;
本文介绍了在iOS项目中如何修改tabBar顶部横线的颜色为#EDEDED,以及如何完全去除横线。通过代码实现,达到自定义tabBar背景色和线条颜色的效果。
7213

被折叠的 条评论
为什么被折叠?



