解決透明NavigationBar底部出現莫名其妙橫線問題

有一個頁面要實現透明NavigationBar,實現了以後發現一個奇怪的問題:第一次進入此頁面顯示透明NavigationBar正常,返回上一級頁面再從新進入,NavigationBar的底部出現了一條大約1px的橫線,怎麼都消除不了。ios

用Reveal查了一下這個橫線,是一個UIImageView,0.5px高度,第一次正常顯示的時候center y座標爲63.75,因此顯示正常;返回上一級頁面時再從新進入,center y座標爲64.25,正好出如今NavigationBar的下面而沒有被它覆蓋。git

猜想是由於要實現透明NavigationBar在其中加了一個64px高度的OverLay View影響了這條橫線的位置。最後在這篇文章中找到了解決方法:github

First – declare instance variable:app

@implementation MyViewController {
    UIImageView *navBarHairlineImageView;}

Then, in viewDidLoad do:ide

navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];

Method which finds the image view we need:ui

- (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:this

- (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..net


另外一種隱藏橫線的方法:rest

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

參考:code

http://stackoverflow.com/questions/19226965/how-to-hide-ios7-uinavigationbar-1px-bottom-line/19227158#19227158

https://github.com/samwize/UINavigationBar-Addition/blob/master/Library/UINavigationBar%2BAddition.m

http://jslim.net/blog/2014/02/28/remove-the-1px-shadow-from-uinavigationbar/

相關文章
相關標籤/搜索