25

Trong ứng dụng của tôi, tôi muốn có thanh điều hướng hiển thị tiêu đề và tiêu đề phụ.Tiêu đề và phụ đề của iPhone trong thanh điều hướng

Trong phạm vi đó, tôi đã thêm đoạn mã sau để điều khiển quan điểm của tôi:

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

Và cũng thực hiện một phương pháp:

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

Mọi thứ làm việc đẹp, cảm ơn.

Ngoại trừ khi xoay iPhone sang ngang, tiêu đề + phụ đề không giảm kích thước theo cách tự động như tiêu đề mặc định của mục điều hướng.

Mọi con trỏ?

Cảm ơn!

+1

Mã của bạn hoạt động tốt và là độc đáo bằng văn bản, Cảm ơn vì đăng. Khi nào thì bạn gọi setHeaderTitle? Tôi đã thử làm điều đó khi viewcontroller được đẩy gọi viewWillAppear nhưng sau đó nó chuyển đổi quá sớm và viewDidAppear chuyển mạch quá muộn. –

+0

Tôi gọi nó theo phương thức init. – Reuven

Trả lời

7

Đặt các nhãn titleViewsubtitleView thành thuộc tính của trình điều khiển chế độ xem để bạn có thể truy cập chúng từ bất kỳ phương thức nào. Sau đó, về luân chuyển các điều khiển xem, thay đổi kích thước nhãn:

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

Cảm ơn - công trình này. (Tôi vẫn cần phát xung quanh với kích thước phông chữ ngang, nhưng hướng hoạt động) – Reuven

1

Thay vì authoresizing này mặt nạ

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

bạn nên sử dụng

_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
             UIViewAutoresizingFlexibleHeight; 

titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
subtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
Các vấn đề liên quan