iPhone Title and Subtitle in Navigation Bar

前端 未结 2 845
灰色年华
灰色年华 2020-12-23 18:39

In my application, I\'d like to have the navigation bar display a title and subtitle.

To that extent, I added the following code to my view controller:



        
相关标签:
2条回答
  • 2020-12-23 19:02

    Make the titleView and subtitleView labels into properties of the view controller, so that you can access them from any method. Then, on rotation of the view controller, resize the labels:

    - (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 讨论(0)
  • 2020-12-23 19:05

    Instead of this authoresizing mask

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

    you should use

    _headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                          UIViewAutoresizingFlexibleHeight;
    
    titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                              UIViewAutoresizingFlexibleHeight;
    subtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                              UIViewAutoresizingFlexibleHeight;
    
    0 讨论(0)
提交回复
热议问题