【问题标题】:Remove Navigationbar's bottom border iOS7移除导航栏底部边框iOS7
【发布时间】:2013-10-03 13:06:06
【问题描述】:

有没有办法删除iOS7在导航栏下自动显示的底部边框?

【问题讨论】:

    标签: objective-c uinavigationbar ios7 xcode5


    【解决方案1】:

    这不适用于 iOS7 导航是否半透明...

    来自 Apple 文档的粘贴;

    说明 用于导航栏的阴影图像。 默认值为 nil,对应于默认的阴影图像。当非零时,此属性表示要显示的自定义阴影图像而不是默认值。要显示自定义阴影图像,还必须使用 setBackgroundImage:forBarMetrics: 方法设置自定义背景图像。如果使用默认背景图片,则无论该属性的值如何,都将使用默认阴影图片。

    所以基本上你需要实现那个 setBackgroundImage。 补充说明,在 iOS7 上,您将不再使用外观,但您将在您现在所在的 viewController 上下文中修改导航栏。

    即:

        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    

    在我的例子中,我把它放在 viewDidLoad 中(可以为 UINavigationViewController 中的每个 UIViewController 添加自定义行为)。

    【讨论】:

    • 感谢您提供更新的回复和文档链接。
    • 就我而言,这是有效的答案,而不是第一个。感谢您提供代码和文档。
    • 在 7+ 上使用外观似乎仍然可以正常工作,但您需要同时设置 shadowImagebackgroundShadowImage
    【解决方案2】:

    如果我理解正确,请尝试

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

    【讨论】:

    • 可能值得注意的是(从 iOS 7 开始)为了使其工作,您还必须执行 [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]。
    • 它在 iOS 7 上对我不起作用。我已将导航栏的半透明属性设置为 false。这可能是个问题吗?
    • @BartSimpson 同时使用 setShadowImage 和 setBackgroundImage。
    【解决方案3】:

    基于 muffed2k answer+ 编程 Thomas 评论, 这就是我用来显示没有背景图像(ios5.1/6.0)和没有底部边框(ios7.0)的 UINavigationBar :

      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
        {
            [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
            [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        }else
        {
            [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        }
    

    【讨论】:

      【解决方案4】:

      如果您使用 Swift 并遇到此问题,请在您的主 ViewController 中尝试:

      override func viewDidLoad() {
          super.viewDidLoad()
      
          /// ...
      
          navigationController?.navigationBar.shadowImage = UIImage();
          navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
      
          //...
      }
      

      根据上面@wolffan 的回答

      【讨论】:

        【解决方案5】:

        translucent 设置为false 时,我在iOS 7 到9+ 上工作过

        UINavigationBar.appearance().transluscent = false
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics:.Default)
        

        【讨论】:

        • 我认为您缺少 .Default 的句点
        【解决方案6】:

        我知道已经有一个公认的答案,但另一种方法是将 clipToBounds 设置为 true。

        这是用 swift 完成的一行代码

        self.navigationController?.navigationBar.clipsToBounds = true
        

        像魅力一样为我工作。

        【讨论】:

        • @user5209290 是正确的。剪辑到导航栏的边界时,我的状态栏不再与导航栏混合。
        【解决方案7】:

        为了目标c

        self.navigationController.navigationBar.clipsToBounds = YES;
        

        【讨论】:

          【解决方案8】:

          像魅力一样工作:Swift 3.x 版本

              navigationController?.navigationBar.shadowImage = UIImage()
              navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
          

          【讨论】:

            【解决方案9】:

            如果您的目标是 iOS 7 并且没有设置背景图片,那么这将起作用:

                    CGFloat navigationBarWidth = self.navigationController.navigationBar.frame.size.width;
                    CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
                    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
            
                    UIGraphicsBeginImageContextWithOptions(CGSizeMake(navigationBarWidth, navigationBarHeight + statusBarHeight), NO, 0.0);
                    UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    [[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
            
                    //the following line takes away the border but only works if a background image is set (above)
                    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
            

            我从@muffe2k 的回答和this SO post 中得到了这个想法。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多