La sottoview UINavigationBar nasconde il giusto UIBarButtonItem
-
06-07-2019 - |
Domanda
Nel controller vista tabella radice aggiungo una sottoview che contiene un'immagine:
[self.navigationController.navigationBar addSubview:imageView];
Quindi in un controller di visualizzazione tabella figlio che inserisco nello stack ho impostato un UIBarButtonItem corretto:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
Non riesco a capire perché il pulsante non sia visualizzato nella barra di navigazione. Se tocco (lo spazio vuoto) il metodo rightAction: viene invocato, quindi il pulsante " è " lì, aspettati che non sia visualizzato.
Ho tentato di inviare nuovamente la sottoview imageView nella barra di navigazione, ma senza risultati. In qualche modo ha senso dato che il pulsante appartiene in realtà all'oggetto UINavigation ...
Qualcuno sa come posso visualizzare correttamente quel pulsante?
Aggiorna: il pulsante Indietro viene visualizzato correttamente ma viene aggiunto dal sistema ...
Soluzione
Un modo migliore per aggiungere un'immagine a un UINavigationBar
è sottoclassarlo o creare una categoria e sovrascrivere il metodo 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
In questo modo, i pulsanti verranno visualizzati correttamente e saranno meno "hacky".
Altri suggerimenti
Penso che dovresti impostare la vista del titolo dell'elemento di navigazione su imageView.
self.navigationItem.titleView = imageView;
Suppongo che UINavigationBar
visualizzi gli UINavigationItem
in una sottoview e tu stavi aggiungendo il tuo UIImageView
in cima a quella subview.
In una delle mie visualizzazioni visualizzo un'etichetta e un'immagine accanto ad essa. Penso di aver appena aggiunto l'etichetta e la vista immagine a una vista che poi ho usato per la vista titolo.