您的位置:首页 > 移动开发 > IOS开发

iOS 修改或去除tabBar顶部横线

2017-12-04 10:21 411 查看
项目中遇到需要修改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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息