请问如何更改导航栏下方的边框颜色?
我想将当前的黑色浅色边框更改为柔和的颜色。 希望得到帮助。
请问如何更改导航栏下方的边框颜色?
我想将当前的黑色浅色边框更改为柔和的颜色。 希望得到帮助。
我认为除了使用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];
在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()
方法中。对于iOS 7,您可以使用以下方法:
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
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];
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
}
}
导航栏
的中间看到它,因此我在旋转期间隐藏了它。@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 上。
y
应该是navigationBar.frame.size.height
,而不是上面提到的navigationBar.frame.size.height - 1
。 - Zhao