您的位置:首页 > 其它

去掉 TabBar 上部横线以及 navigationBar 下部横线的方法探讨

2015-09-21 13:36 369 查看
在一些应用中需要将 TabBar 上部横线或者 navigationBar 下部横线取消掉,之前也尝试了多种方法,现将个人目前使用的方法列举如下,各位看官若是有更好的方案欢迎补充。

tabBar:

直接将系统 tabBar 中所有的控件打印输出,会发现有如下控件(数量依据标签栏的数量不同而不同)

(
"<_UITabBarBackgroundView: 0x7f99d1f1bc20; frame = (0 0; 414 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7f99d1f00720>>",
"<UITabBarButton: 0x7f99d1d540f0; frame = (2 1; 203 48); opaque = NO; layer = <CALayer: 0x7f99d1d56010>>",
"<UITabBarButton: 0x7f99d1d5a060; frame = (209 1; 203 48); opaque = NO; layer = <CALayer: 0x7f99d1d59cc0>>",
"<UIImageView: 0x7f99d1f1da30; frame = (0 -0.333333; 414 0.333333); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7f99d1f1dbd0>>"
)


再结合Xcode自带的查看视图层次结构的功能可以看到上部的那根横线其实就是 UIImageView ,所以若是自定义或者将其去掉直接修改这个控件既可。



代码实现如下:

/// 隐藏底部 TabBar 的上横线
for (UIView *view in self.tabBar.subviews) {

if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1) {
UIImageView *ima = (UIImageView *)view;
//            ima.backgroundColor = [UIColor redColor];
ima.hidden = YES;
}
}
效果如下:



navigationBar:

navigationBar 常用的方案有两个:

第一个是直接修改但是半透明穿透效果就没有了;

第二个是利用遍历子控件+递归来判断拿到那个控件对其进行修改,半透明效果依然存在。

第一种:

<span style="font-size:14px;">[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];</span>


第二种:
我们先将navigationBar 中的控件输出来看一下

<span style="font-size:14px;">(
"<_UINavigationBarBackground: 0x7fd6f0f2b430; frame = (0 -20; 414 64); opaque = NO; autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd6f0f2b1a0>>",
"<_UINavigationBarBackIndicatorView: 0x7fd6f0d3dee0; frame = (0 11.6667; 13 21); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd6f0d3e300>>"
)</span><span style="font-size:24px;">
</span>


会发现并没有 UIImageView 这个控件,但是查看视图层次结构图



我们会发现下部的那根横线其实还是一个 UIImageView ,并借助视图层次结构图我们会发现其实是子控件又包含有子控件的形式存在着,所以我们要拿到对应的控件就要进行递归遍历的查找了,代码实现如下:

<span style="font-size:14px;">@interface ViewController ()

@property (nonatomic, strong) UIImageView *navigationBarBottomLine;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 拿到对应的控件
self.navigationBarBottomLine = [self findBottomLineUnder:self.navigationController.navigationBar];
}

- (UIImageView *)findBottomLineUnder:(UIView *)view {
// 符合条件返回控件
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
// 递归查找
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findBottomLineUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationBarBottomLine.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationBarBottomLine.hidden = NO;
}</span>

效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: