簡體   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