2011-07-25 22 views

Respuesta

41

No creo que haya un método para cambiar el color del borde del color de navegación, que no sea la propiedad tintColor del UINavigationBar.

Le sugiero que cree una UIView de ese tamaño de borde y la coloque debajo de la barra de navegación/agréguela como 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

genial! Esto funciona para mí :) ¡Gracias! – Zhen

2

También puede añadir una vista de niño a su barra de navegación

El siguiente código añade un profundo borde azul 4 píxeles por debajo de la barra de navegació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

pesar navigationbar es de sólo lectura propiedad para UINavigationController puede evitar esta restricción mediante "setValue: forKey:". Este método fue aprobado en 5 aplicaciones enviadas con éxito a AppStore.

Puede subclase UINavigationBar y cambiar el método drawRect: como desee. Por ejemplo,

@implementation CustomNavigationBar 

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

Después puede subclase UINavigationController y cambiar initWithRootViewController:

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

También se puede variar este enfoque, haciendo Categoría de UINavigationController e implementar el método swizzling para initWithRootViewController:

P. S. Ayer, mi nueva aplicación apareció en AppStore sin ningún problema con este enfoque.

4

Para iOS 7 podría utilizar este:

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

En Swift como la respuesta @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) 
} 

Se puede retener en el viewDidLoad() de su UIViewController.

+2

La 'y' del origen del marco de vista debe ser' navigationBar.frame.size.height' en lugar de 'navigationBar.frame.size.height - 1' mencionada anteriormente. – Zhao

1

me encontré con un par de problemas con las respuestas anteriores:

  • si se agrega una vista secundaria: Al girar el dispositivo, la frontera no tendrá el marco correcto más
  • si se establece la ShadowImage a nil, entonces no se puede personalizar la sombra de la barra de navegación.

Una forma de resolver este sería el uso de diseño automático:

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 
    } 
} 

La razón por la que vuelvo la frontera es que durante la rotación usted lo ve en el medio de la navigation bar, así que lo oculta durante la rotación.

Cuestiones relacionadas