iPhone titre et sous-Titre dans la Barre de Navigation
Dans mon application, j'aimerais avoir la barre de navigation pour afficher un titre et des sous-titres.
Dans cette mesure, j'ai ajouté le code suivant à mon point de vue, contrôleur:
//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;
Et également mis en œuvre une méthode:
-(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;
}
Les choses fonctionnent très bien, merci.
Sauf que lors de la rotation de l'iPhone en mode Paysage, le titre+sous-titre ne pas réduire dans un, de manière automatique, comme le titre par défaut de l'élément de navigation.
Les pointeurs?
Merci!
- Votre code fonctionne bien et est bien écrit, merci pour l'affichage. Quand êtes-vous exactement appelant setHeaderTitle? J'ai essayé de le faire quand la poussée viewcontroller appels viewWillAppear mais ensuite il passe trop tôt et viewDidAppear commutateurs trop tard.
- Je l'appelle dans l'init méthodes.
InformationsquelleAutor Reuven | 2010-05-12
Vous devez vous connecter pour publier un commentaire.
Faire la
titleView
etsubtitleView
des étiquettes dans les propriétés de la vue-contrôleur, de sorte que vous pouvez accéder à partir de n'importe quelle méthode. Puis, lors de la rotation de la vue-contrôleur, redimensionner les étiquettes:Au lieu de cela authoresizing masque
vous devez utiliser