iOS7 - 更改UINavigationBar边框颜色

Map*_*Dev 29 border colors uinavigationbar ios7 xcode5

是否可以在iOS7中更改UINavigationBar的灰色边框底部颜色?

我已经尝试删除到边框,但这不起作用:

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
Run Code Online (Sandbox Code Playgroud)

谢谢!

nul*_*ull 62

您正在删除阴影但不删除边框,您需要执行以下操作:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
Run Code Online (Sandbox Code Playgroud)

要更改边框,请使用2像素宽度线的图像:

[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"2pxWidthLineImage"]]; 
Run Code Online (Sandbox Code Playgroud)

  • ```setShadowImage```似乎不再存在. (2认同)

sas*_*ash 55

这是一个用高度改变底色的类别:

[self.navigationController.navigationBar setBottomBorderColor:[UIColor redColor] height:1];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

目标C:

UINavigationBar的+ Helper.h

#import <UIKit/UIKit.h>

@interface UINavigationBar (Helper)
- (void)setBottomBorderColor:(UIColor *)color height:(CGFloat)height;
@end
Run Code Online (Sandbox Code Playgroud)

UINavigationBar的+ Helper.m

#import "UINavigationBar+Helper.h"

@implementation UINavigationBar (Helper)

- (void)setBottomBorderColor:(UIColor *)color height:(CGFloat)height {
    CGRect bottomBorderRect = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), height);
    UIView *bottomBorder = [[UIView alloc] initWithFrame:bottomBorderRect];
    [bottomBorder setBackgroundColor:color];
    [self addSubview:bottomBorder];
}
@end
Run Code Online (Sandbox Code Playgroud)

迅速:

extension UINavigationBar {

    func setBottomBorderColor(color: UIColor, height: CGFloat) {
        let bottomBorderRect = CGRect(x: 0, y: frame.height, width: frame.width, height: height)
        let bottomBorderView = UIView(frame: bottomBorderRect)
        bottomBorderView.backgroundColor = color
        addSubview(bottomBorderView)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Vahid我认为你应该在旋转完成后调用这个方法,它会重绘`subViews`.或者只是重新加载`subViews`. (2认同)

k06*_*06a 9

这是另一种方式:

CALayer *border = [CALayer layer];
border.borderColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"border"]].CGColor;
border.borderWidth = 1;
CALayer *layer = self.navigationController.navigationBar.layer;
border.frame = CGRectMake(0, layer.bounds.size.height, layer.bounds.size.width, 1);
[layer addSublayer:border];
Run Code Online (Sandbox Code Playgroud)


Luc*_*nzo 6

我发现改变颜色的唯一方法是:

override func viewDidLoad() {
    super.viewDidLoad()

    if let navigationController = self.navigationController {
        let navigationBar = navigationController.navigationBar
        let navigationSeparator = UIView(frame: CGRectMake(0, navigationBar.frame.size.height - 1, navigationBar.frame.size.width, 0.5))
        navigationSeparator.backgroundColor = UIColor.redColor() // Here your custom color
        navigationSeparator.opaque = true
        self.navigationController?.navigationBar.addSubview(navigationSeparator)
    }

}
Run Code Online (Sandbox Code Playgroud)


FTF*_*234 5

我根据其他答案编写了一个扩展,以便在Swift中更容易使用:

extension UINavigationBar {

    func setBottomBorderColor(color: UIColor) {

        let navigationSeparator = UIView(frame: CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5))
        navigationSeparator.backgroundColor = color
        navigationSeparator.opaque = true
        navigationSeparator.tag = 123
        if let oldView = self.viewWithTag(123) {
            oldView.removeFromSuperview()
        }
        self.addSubview(navigationSeparator)

    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在以下上下文中调用方法来使用此扩展:

self.navigationController?.navigationBar.setBottomBorderColor(UIColor.whiteColor())
Run Code Online (Sandbox Code Playgroud)

我发现这非常有用,因为我必须处理那个彩色边框问题.


Nik*_*nko 5

我通过使用自动布局解决了这个问题。该解决方案适用于不同的屏幕尺寸以及方向变化。

extension UINavigationBar {

    @IBInspectable var bottomBorderColor: UIColor {
        get {
            return self.bottomBorderColor;
        }
        set {
            let bottomBorderRect = CGRect.zero;
            let bottomBorderView = UIView(frame: bottomBorderRect);
            bottomBorderView.backgroundColor = newValue;
            addSubview(bottomBorderView);

            bottomBorderView.translatesAutoresizingMaskIntoConstraints = false;

            self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0));
            self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0));
            self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0));
            self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 1));
        }

    }

}
Run Code Online (Sandbox Code Playgroud)


Adn*_*tab -11

这会对你有帮助:)

[self.navigationController.navigationBar.layer setBorderWidth:2.0];// Just to make sure its working
[self.navigationController.navigationBar.layer setBorderColor:[[UIColor redColor] CGColor]];
Run Code Online (Sandbox Code Playgroud)

  • 这是整个 UINavigation 栏周围的边框,这不是边框作者试图更改的。当尝试设置底部边框时,它没有帮助。 (14认同)