La subvista UINavigationBar está ocultando el elemento UIBarButtonItem correcto
-
06-07-2019 - |
Pregunta
En el controlador de vista de tabla raíz agrego una subvista que contiene una imagen:
[self.navigationController.navigationBar addSubview:imageView];
Luego, en un controlador de vista de tabla hijo que inserto en la pila, configuré un UIBarButtonItem correcto:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
No puedo entender por qué el botón no se muestra en la barra de navegación. Si toco (el espacio en blanco), se invoca el método rightAction: por lo que el botón '' es '' allí, espere que no se muestre.
He intentado enviar la subvista imageView a la barra de navegación, pero fue en vano. Tiene sentido, ya que el botón realmente pertenece al UINavigationItem de todos modos ...
¿Alguien sabe cómo puedo hacer que ese botón se muestre correctamente?
Actualización: el botón de retroceso se muestra correctamente pero el sistema lo agrega ...
Solución
Una mejor manera de agregar una imagen a una UINavigationBar
es subclasificarla o hacer una categoría y anular el método drawRect
:
// #Lighter r,g,b,a #Darker r,g,b,a
#define MAIN_COLOR_COMPONENTS { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
#define LIGHT_COLOR_COMPONENTS { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
if (imageReady) {
UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
} else {
// Render yourself instead.
// You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app
// emulate the tint colored bar
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat locations[2] = { 0.0, 1.0 };
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
CGGradientRelease(topGradient);
CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
CGContextDrawLinearGradient(context, botGradient,
CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
CGGradientRelease(botGradient);
CGColorSpaceRelease(myColorspace);
// top Line
CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, self.frame.size.width, 0);
CGContextStrokePath(context);
// bottom line
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextMoveToPoint(context, 0, self.frame.size.height);
CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(context);
}
}
@end
Hacerlo de esta manera hará que tus botones se muestren correctamente y sea menos "hacky".
Otros consejos
Creo que debería configurar la vista de título del elemento de navegación en su imageView.
self.navigationItem.titleView = imageView;
Supongo que el UINavigationBar
muestra los UINavigationItem
s en una subvista, y estaba agregando su UIImageView
encima de esa subvista.
En una de mis vistas, estoy mostrando una etiqueta y una imagen al lado. Creo que acabo de agregar la etiqueta y la vista de imagen a una vista que luego utilicé para titleView.