zoukankan      html  css  js  c++  java
  • 设置uinavigationbar导航条透明,并去除下面那条莫名其妙的线

    For a custom shadow image to be shown, custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

    So the code is:

    1 UINavigationBar *navigationBar = self.navigationController.navigationBar;
    2 
    3 [navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"]
    4                    forBarPosition:UIBarPositionAny
    5                        barMetrics:UIBarMetricsDefault];
    6 
    7 [navigationBar setShadowImage:[UIImage new]];

    This code assumes you want the image named "NavigationBarBackground" as bar background. If it's not the case, you can make the background a solid color by setting backgroundImage to [UIImage new] and assigning navigationBar.backgroundColor to the color you like.

    Above is the only "official" way to hide it. But unfortunately it removes bar's translucency.

    How to keep bar translucent?

    To keep translucency you need another approach, it looks like a hack but works well. The hairline we're trying to remove is UIImageView somewhere under UINavigationBar. So we have to find it and hide or show it when needed.

    First – declare instance variable:

    1 @implementation MyViewController {
    2     UIImageView *navBarHairlineImageView;
    3 }

    Then, in viewDidLoad do:

    1 navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];

    Method which finds the image view we need:

     1 - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
     2     if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
     3             return (UIImageView *)view;
     4     }
     5     for (UIView *subview in view.subviews) {
     6         UIImageView *imageView = [self findHairlineImageViewUnder:subview];
     7         if (imageView) {
     8             return imageView;
     9         }
    10     }
    11     return nil;
    12 }

    And this will do the rest of magic:

    1 - (void)viewWillAppear:(BOOL)animated {
    2     [super viewWillAppear:animated];
    3     navBarHairlineImageView.hidden = YES;
    4 }
    5 
    6 - (void)viewWillDisappear:(BOOL)animated {
    7     [super viewWillDisappear:animated];
    8     navBarHairlineImageView.hidden = NO;
    9 }

    Same method should also work for UISearchBar hairline.

  • 相关阅读:
    31天重构学习笔记28. 为布尔方法命名
    .NET 技术社区之我见(中文篇)
    31天重构学习笔记26. 避免双重否定
    31天重构学习笔记25. 引入契约式设计
    31天重构学习笔记20. 提取子类
    31天重构学习笔记18. 使用条件判断代替异常
    31天重构学习笔记19. 提取工厂类
    31天重构学习笔记24. 分解复杂判断
    31天重构学习笔记23. 引入参数对象
    31天重构学习笔记17. 提取父类
  • 原文地址:https://www.cnblogs.com/helmsyy/p/4964626.html
Copyright © 2011-2022 走看看