problema NavigationBarStyle quando si passa da IOS 4 a 5
-
26-10-2019 - |
Domanda
avere un po 'problema di progettazione, dopo aver eseguito l'aggiornamento a iOS 5 e Xcode 4.2
Ecco come il mio vista sembrava in iOS 4:
E questo è come sembra in iOS 5:
Nel mio delegato di navigazione ho il seguente metodo per disegnare l ' "immagine" nella parte superiore:
- (void)drawRect:(CGRect)rect {
UIImage *image;
if(self.barStyle == UIBarStyleDefault){
image = [UIImage imageNamed: @"topbar_base.png"];
}
else{
image = [UIImage imageNamed: @"nyhedsbar_base.png"];
}
[image drawInRect:CGRectMake(-1, -1, self.frame.size.width+3, self.frame.size.height+3)];
}
E dentro il mio controller ho impostato il seguente:
self.navigationBarStyle = UIBarStyleBlack;
Come mai non funziona in iOS 5?
Grazie
Soluzione
Under iOS5, you need to use UIAppearance
. Have a look at that. Here's an example for using it conditionally so that you can continue to support iOS4:
// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
UIImage *img = [UIImage imageNamed: @"NavBarBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
As you can see, this sets a custom background image for all UINavigationBars. There are lots of things you can do with UIAppearance. You'll want to keep any custom stuff you're currently doing in drawRect:
since pre-iOS4 devices will still use that and not the new UIAppearance code.