美文网首页
iOS 完全透明navigationBar以及隐藏透明后的黑色线

iOS 完全透明navigationBar以及隐藏透明后的黑色线

作者: LikeSmilence | 来源:发表于2016-04-01 16:38 被阅读712次

    页面要实现透明NavigationBar使用下面的方法

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
    

    让tableView之类的起始点在最上方设置

    self.automaticallyAdjustsScrollViewInsets = NO;
    

    接下来是navigationBar透明后黑线的问题

    @interface MyViewController () {
        UIImageView *navBarHairlineImageView;
    }
    @end
    @implementation MyViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
        navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    - (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;
    }
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        navBarHairlineImageView.hidden = YES;
    }
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        navBarHairlineImageView.hidden = NO;   
    }
    

    也可以用更简单的

    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
            NSArray *list = self.navigationController.navigationBar.subviews;
            for (id obj in list) {
                if ([obj isKindOfClass:[UIImageView class]]) {
                    UIImageView *imageView = (UIImageView *)obj;
                    NSArray *list2 = imageView.subviews;
                    for (id obj2 in list2) {
                        if ([obj2 isKindOfClass:[UIImageView class]]) {
                            UIImageView *imageView2 = (UIImageView *)obj2;
                            imageView2.hidden = YES;
                        }
                    }
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:iOS 完全透明navigationBar以及隐藏透明后的黑色线

          本文链接:https://www.haomeiwen.com/subject/resylttx.html