繁体   English   中英

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

[英]How to change the border color below the navigation bar?

任何人都可以告诉我如何更改导航栏下方的边框?

在此输入图像描述

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

我不认为有一种方法可以改变导航颜色的边框颜色,而不是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];

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

对于iOS 7,您可以使用:

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

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

以下代码将在导航栏下方添加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];

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

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

解决这个问题的一种方法是使用autolayout:

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的中间看到它,所以我在旋转过程中隐藏了它。

尽管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创建Category并为initWithRootViewController实现方法调整来改变这种方法:

PS昨天我的新应用程序出现在AppStore,这种方法没有任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM