【问题标题】:How to change the border color below the navigation bar?如何更改导航栏下方的边框颜色?
【发布时间】:2011-07-25 10:58:28
【问题描述】:

谁能告诉我如何更改导航栏下方的边框?

我想将它从当前的黑光更改为更柔和的颜色。 在这里感谢任何帮助

【问题讨论】:

    标签: iphone objective-c ios uinavigationbar border


    【解决方案1】:

    我认为除了UINavigationBar 的tintColor 属性之外,没有其他方法可以更改导航颜色的边框颜色。

    我建议您创建一个具有该边框大小的 UIView 并将其放在导航栏下方/将其添加为 subView。

    UIView *navBorder = [[UIView alloc] initWithFrame:CGRectMake(0,navigationBar.frame.size.height-1,navigationBar.frame.size.width, 1)]; 
    
    // Change the frame size to suit yours //
    
    [navBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:0.8f]];
    [navBorder setOpaque:YES];
    [navigationBar addSubview:navBorder];
    [navBorder release];
    

    【讨论】:

      【解决方案2】:

      在 Swift 中就像 @Legolas 的回答:

      if let navigationController = self.navigationController {
      
         let navigationBar = navigationController.navigationBar     
         let navBorder: UIView = UIView(frame: CGRectMake(0, navigationBar.frame.size.height - 1, navigationBar.frame.size.width, 1))
         // Set the color you want here
         navBorder.backgroundColor = UIColor(red: 0.19, green: 0.19, blue: 0.2, alpha: 1)
         navBorder.opaque = true
         self.navigationController?.navigationBar.addSubview(navBorder)
      }
      

      您可以输入您的UIViewController 的viewDidLoad()。

      【讨论】:

      • view frame origin的y应该是navigationBar.frame.size.height而不是上面提到的navigationBar.frame.size.height - 1。
      【解决方案3】:

      对于 iOS 7,你可以使用这个:

      [self.navigationController.navigationBar setShadowImage:[UIImage new]];
      

      【讨论】:

        【解决方案4】:

        您还可以向导航栏添加子视图

        以下代码将在您的导航栏下方添加一个 4 像素的深蓝色边框

        UINavigationBar* navBar = self.navigationController.navigationBar;
            int borderSize = 4;
            UIView *navBorder = [[UIView alloc] initWithFrame:CGRectMake(0,navBar.frame.size.height-borderSize,navBar.frame.size.width, borderSize)];
            [navBorder setBackgroundColor:[UIColor blueColor]];
            [self.navigationController.navigationBar addSubview:navBorder];
        

        【讨论】:

          【解决方案5】:

          我发现之前的答案有几个问题:

          • 如果添加子视图:设备旋转后,边框将不再具有正确的框架
          • 如果将 shadowImage 设置为 nil,则无法自定义 navigationBar 的阴影。

          解决这个问题的一种方法是使用自动布局:

          extension UINavigationBar {
          
            func setBottomBorderColor(color: UIColor, height: CGFloat) -> UIView {
              let bottomBorderView = UIView(frame: CGRectZero)
              bottomBorderView.translatesAutoresizingMaskIntoConstraints = false
              bottomBorderView.backgroundColor = color
          
              self.addSubview(bottomBorderView)
          
              let views = ["border": bottomBorderView]
              self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[border]|", options: [], metrics: nil, views: views))
              self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height))
              self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: height))
          
              return bottomBorderView
            }
          }
          

          我返回边框的原因是在旋转过程中你确实看到它在navigation bar的中间,所以我在旋转过程中隐藏了它。

          【讨论】:

            【解决方案6】:

            尽管 navigationBar 是 UINavigationController 的只读属性,但您可以避免 这个限制由“setValue:forKey:”决定。此方法已在 5 个成功提交到 AppStore 的应用程序上获得批准。

            您可以继承 UINavigationBar 并根据需要更改 drawRect: 方法。 例如,

            @implementation CustomNavigationBar
            
            - (void) drawRect:(CGRect)rect
            {
                [super drawRect:rect];
                UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
                [backgroundImage drawInRect:rect];
            }
            

            在您可以继承 UINavigationController 并更改 initWithRootViewController 之后:

            - (id) initWithRootViewController:(UIViewController *)rootViewController
            {
                self = [super initWithRootViewController:rootViewController]; 
                if (self) 
                {
                    CustomNavigationBar *navBar = [CustomNavigationBar new];
                    [self setValue:navBar forKey:@"navigationBar"];
                }
                return self;
            }
            

            您还可以通过为 UINavigationController 制作 Category 并为 initWithRootViewController 实现方法调整来改变这种方法:

            附:昨天我的新应用出现在 AppStore 上,这种方法没有任何问题。

            【讨论】:

              猜你喜欢
              • 2015-09-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-02-06
              • 2020-12-20
              • 1970-01-01
              相关资源
              最近更新 更多