【问题标题】:iPhone Title and Subtitle in Navigation Bar导航栏中的 iPhone 标题和副标题
【发布时间】:2011-02-18 12:27:11
【问题描述】:

在我的应用程序中,我想让导航栏显示标题和副标题。

为此,我在视图控制器中添加了以下代码:

// 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;

并且还实现了一个方法:

-(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;
}

一切都很好,谢谢。

除了将iPhone旋转为横向时,标题+副标题不会像导航项的默认标题那样自动缩小。

任何指针?

谢谢!

【问题讨论】:

  • 您的代码运行良好,写得很好,感谢您的发布。你到底什么时候打电话给 setHeaderTitle?当推送的视图控制器调用 viewWillAppear 时,我尝试过这样做,但是它切换得太早并且 viewDidAppear 切换得太晚了。
  • 我在init方法中调用它。

标签: iphone uinavigationcontroller title uinavigationitem subtitle


【解决方案1】:

titleViewsubtitleView 标签设置为视图控制器的属性,以便您可以通过任何方法访问它们。然后,在视图控制器旋转时,调整标签的大小:

- (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];
    }
}

【讨论】:

  • 谢谢 - 这行得通。 (我仍然需要使用横向字体大小,但方向有效)
【解决方案2】:

而不是这个授权掩码

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

你应该使用

_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                      UIViewAutoresizingFlexibleHeight;

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

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2011-01-09
    • 1970-01-01
    • 2011-01-17
    • 2011-04-23
    • 1970-01-01
    • 2011-11-15
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多