25

Nella mia applicazione, mi piacerebbe che la barra di navigazione mostri un titolo e un sottotitolo.Titolo iPhone e sottotitoli nella barra di navigazione

In tal senso, ho aggiunto il seguente codice per il mio controller della vista:

// Replace titleView 
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);  
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease]; 
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor]; 
_headerTitleSubtitleView.autoresizesSubviews = YES; 

CGRect titleFrame = CGRectMake(0, 2, 200, 24); 
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease]; 
titleView.backgroundColor = [UIColor clearColor]; 
titleView.font = [UIFont boldSystemFontOfSize:20]; 
titleView.textAlignment = UITextAlignmentCenter; 
titleView.textColor = [UIColor whiteColor]; 
titleView.shadowColor = [UIColor darkGrayColor]; 
titleView.shadowOffset = CGSizeMake(0, -1); 
titleView.text = @""; 
titleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:titleView]; 

CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24); 
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease]; 
subtitleView.backgroundColor = [UIColor clearColor]; 
subtitleView.font = [UIFont boldSystemFontOfSize:13]; 
subtitleView.textAlignment = UITextAlignmentCenter; 
subtitleView.textColor = [UIColor whiteColor]; 
subtitleView.shadowColor = [UIColor darkGrayColor]; 
subtitleView.shadowOffset = CGSizeMake(0, -1); 
subtitleView.text = @""; 
subtitleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:subtitleView]; 

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleTopMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 

self.navigationItem.titleView = _headerTitleSubtitleView; 

E anche implementato un metodo:

-(void) setHeaderTitle:(NSString*)headerTitle andSubtitle:(NSString*)headerSubtitle { 
    assert(self.navigationItem.titleView != nil); 
    UIView* headerTitleSubtitleView = self.navigationItem.titleView; 
    UILabel* titleView = [headerTitleSubtitleView.subviews objectAtIndex:0]; 
    UILabel* subtitleView = [headerTitleSubtitleView.subviews objectAtIndex:1]; 
    assert((titleView != nil) && (subtitleView != nil) && ([titleView isKindOfClass:[UILabel class]]) && ([subtitleView isKindOfClass:[UILabel class]])); 
    titleView.text = headerTitle; 
    subtitleView.text = headerSubtitle; 
} 

Le cose funzionano in modo bello, grazie.

Tranne che quando si ruota l'iPhone in Orizzontale, il titolo + sottotitolo non ridimensiona in modo automatico come il titolo predefinito della voce di navigazione.

Eventuali suggerimenti?

Grazie!

+1

Il codice funziona bene ed è ben scritto, grazie per la pubblicazione Quando stai chiamando setHeaderTitle? Ho provato a farlo quando il viewcontroller premuto chiama viewWillAppear ma poi passa troppo presto e viewDidAppear passa troppo tardi. –

+0

Lo chiamo nei metodi init. – Reuven

risposta

7

Creare le etichette titleView e subtitleView nelle proprietà del controller di visualizzazione, in modo da poterle accedere da qualsiasi metodo. Poi, sulla rotazione del controller della vista, ridimensionare le etichette:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustLabelsForOrientation:toInterfaceOrientation]; 
} 

- (void) adjustLabelsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
     titleView.font = [UIFont boldSystemFontOfSize:16]; 
     subtitleView.font = [UIFont boldSystemFontOfSize:11]; 
    } 
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     titleView.font = [UIFont boldSystemFontOfSize:20]; 
     subtitleView.font = [UIFont boldSystemFontOfSize:13]; 
    } 
} 
+0

Grazie - questo funziona. (Ho ancora bisogno di giocare con le dimensioni del carattere orizzontale, ma la direzione funziona) – Reuven

1

Invece di questo authoresizing maschera

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleTopMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 

si dovrebbe usare

_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
             UIViewAutoresizingFlexibleHeight; 

titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
subtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
Problemi correlati