如何更改导航栏下方的边框颜色?

15

请问如何更改导航栏下方的边框颜色?

enter image description here

我想将当前的黑色浅色边框更改为柔和的颜色。 希望得到帮助。

6个回答

43

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

我建议您创建一个大小相同的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];

9

在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)
}

你可以将代码放在你的UIViewControllerviewDidLoad()方法中。

2
视图框架原点的 y 应该是 navigationBar.frame.size.height,而不是上面提到的 navigationBar.frame.size.height - 1 - Zhao

4

对于iOS 7,您可以使用以下方法:

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

2
你可以在导航栏中添加一个子视图。
以下代码将在你的导航栏下方添加一个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];

1
我发现之前的答案有一些问题:
  • 如果你添加了一个子视图:设备旋转后,边框将不再具有正确的框架
  • 如果你将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
  }
}

我之所以返回边框,是因为在旋转过程中,您会在 导航栏 的中间看到它,因此我在旋转期间隐藏了它。

-1
尽管navigationBar是UINavigationController的只读属性,但你可以通过"setValue:forKey:"来避免这个限制。这种方法已经在成功提交到AppStore的5个应用程序中得到了验证。
你可以子类化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 制作类别并实现 initWithRootViewController: 的方法混淆来改变这种方法:

P.S. 昨天我使用该方法发布了我的新应用程序,没有任何问题出现在 AppStore 上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接