iOS 修改或去除tabBar顶部横线
2017-12-04 10:21
411 查看
项目中遇到需要修改tabBar顶部横线的颜色,去网上找了好多 效果都不太理想 于是总结了一下 方便日后查阅
![](https://img-blog.csdn.net/20171204100433924?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWUpMaU9T/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
需要实现的效果是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)
完成之后的效果如下:
![](https://img-blog.csdn.net/20171204101852523?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWUpMaU9T/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20171204101908190?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWUpMaU9T/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
去除tabBar顶部横线的方法
self.tabBarViewController.tabBar.clipsToBounds = YES;
需要实现的效果是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]iOS7+ UITabBar修改选中图片显示为默认蓝色的问题解决方法,去除UITabbar顶部一像素横线
- iOS去除导航栏和tabbar的1px横线
- 去除导航栏和 tabbar 的1px 横线 - iOS
- iOS去除导航栏和tabbar的1px横线
- iOS去除导航栏和tabbar的1px横线
- IOS开发教程--如何去除tabbar顶部线条及设置背景图片
- iOS去除导航栏和tabbar的横线
- iOS 去掉tabbar顶部的横线
- iOS去除导航栏和tabbar的1px横线
- iOS - 如何去除tabbar顶部线条及设置背景图片
- iOS - 如何去除tabbar顶部线条及设置背景图片
- iOS更改tabbar图片渲染 —不让tabbat有蓝色的渲染 并修改文字
- iOS 修改TabBar的高度
- 【iOS】去掉Tabbar顶部线条
- iOS去除导航栏和tabbar的1px横线
- IOS开发—无需自定义,修改tabbar背景颜色
- IOS UIlabel自适应Fram后顶部出现灰色横线
- TabBar去除顶部黑线和NavigationBar去除底部黑线
- iOS 顶部导航栏字体、颜色 底部tabbar颜色
- iOS顶部 图片 文字 button (底部横线选中) 切换