2011-07-25 54 views

Trả lời

41

Tôi không nghĩ rằng có một phương pháp để thay đổi màu đường viền của màu điều hướng, ngoại trừ thuộc tính tintColor của UINavigationBar.

Tôi khuyên bạn nên tạo UIView kích thước đường viền đó và đặt nó bên dưới thanh điều hướng/thêm nó dưới dạng 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]; 
+0

tuyệt vời! Những công trình cho tôi :) Cảm ơn! – Zhen

2

Bạn cũng có thể thêm một cái nhìn trẻ thanh nav của bạn

Đoạn mã dưới đây sẽ thêm một 4 pixel đường viền màu xanh sâu bên dưới thanh điều hướng của bạn

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

Mặc dù Navigationbar là read-only tài sản cho UINavigationController bạn có thể tránh hạn chế này bằng "setValue: forKey:". Phương thức này đã được phê duyệt trên 5 ứng dụng được gửi thành công tới AppStore.

Bạn có thể phân lớp UINavigationBar và thay đổi drawRect: phương thức như bạn muốn. Ví dụ,

@implementation CustomNavigationBar 

- (void) drawRect:(CGRect)rect 
{ 
    [super drawRect:rect]; 
    UIImage *backgroundImage = ImageFromColor(WANTED_COLOR); 
    [backgroundImage drawInRect:rect]; 
} 

Sau khi bạn có thể phân lớp UINavigationController và thay đổi initWithRootViewController:

- (id) initWithRootViewController:(UIViewController *)rootViewController 
{ 
    self = [super initWithRootViewController:rootViewController]; 
    if (self) 
    { 
     CustomNavigationBar *navBar = [CustomNavigationBar new]; 
     [self setValue:navBar forKey:@"navigationBar"]; 
    } 
    return self; 
} 

Ngoài ra bạn có thể thay đổi phương pháp này bằng cách loại cho UINavigationController và thực hiện các phương pháp swizzling cho initWithRootViewController:

T.B. Hôm qua, ứng dụng mới của tôi đã xuất hiện tại AppStore mà không có bất kỳ vấn đề gì với phương pháp này.

4

Đối với iOS 7, bạn có thể sử dụng này:

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

Trong Swift như câu trả lời @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) 
} 

Bạn có thể đặt trong viewDidLoad() của UIViewController của bạn.

+2

'y' của khung nhìn gốc nên là' navigationBar.frame.size.height' thay vì 'navigationBar.frame.size.height - 1' được đề cập ở trên. – Zhao

1

Tôi tìm thấy một vài vấn đề với các câu trả lời trước:

  • nếu bạn thêm một subview: Khi quay của thiết bị, biên giới sẽ không có khung đúng nữa
  • nếu bạn đặt ShadowImage để không, sau đó bạn không thể tùy chỉnh bóng của navigationBar.

Một cách giải quyết này sẽ được sử dụng 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 
    } 
} 

Lý do tại sao tôi trở lại biên giới là trong quá trình luân chuyển bạn nhìn thấy nó ở giữa các navigation bar, vì vậy tôi giấu nó trong vòng quay.

Các vấn đề liên quan