有一个页面要实现透明NavigationBar,实现了之后发现一个奇怪的问题:第一次进入此页面显示透明NavigationBar正常,返回上一级页面再重新进入,NavigationBar的底部出现了一条大约1px的横线,怎么都消除不了。
用Reveal查了一下这个横线,是一个UIImageView,0.5px高度,第一次正常显示的时候center y坐标为63.75,所以显示正常;返回上一级页面时再重新进入,center y坐标为64.25,正好出现在NavigationBar的下面而没有被它覆盖。
猜测是因为要实现透明NavigationBar在其中加了一个64px高度的OverLay View影响了这条横线的位置。最后在这篇文章中找到了解决方法:
First – declare instance variable:
@implementation MyViewController { UIImageView *navBarHairlineImageView;}
Then, in viewDidLoad
do:
navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];
Method which finds the image view we need:
- (UIImageView *)findHairlineImageViewUnder:(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 findHairlineImageViewUnder:subview]; if (imageView) { return imageView; } } return nil;}
And this will do the rest of magic:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; navBarHairlineImageView.hidden = YES;}- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; navBarHairlineImageView.hidden = NO;}
Same method should also work for UISearchBar
hairline.
时间: 2024-11-07 12:06:32