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

Zhe*_*hen 15 iphone border objective-c uinavigationbar ios

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

在此输入图像描述

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

Leg*_*las 42

我不认为有一种方法可以改变导航颜色的边框颜色,除了tintColor属性UINavigationBar.

我建议您创建一个边框大小的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];
Run Code Online (Sandbox Code Playgroud)


Vic*_*ler 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)
}
Run Code Online (Sandbox Code Playgroud)

你可以把viewDidLoad()你的UIViewController.

  • 视图框原点的`y`应该是`navigationBar.frame.size.height`而不是上面提到的`navigationBar.frame.size.height - 1`. (2认同)