How to set large title on navigation bar for iOS 1

2019-09-20 16:26发布

问题:

I am creating app for iOS 11 or later, requirement to set the large title on left side of navigation bar.

Please someone help to sort out how to set and it should work only for iOS 11 or later.

Give me some other suggestions to maintain this feature available through out app (which support iOS 8 or later).

Thanks in advance.

回答1:

Here is code snippet to display large title on left side of navigation bar for iOS 11 or later.

Objective C:

  self.title = @"Your title";
    if (@available(iOS 11, *)) {
        self.navigationController.navigationBar.prefersLargeTitles = true;
         self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
    }

Swift:

       self.title = "Your title"
        if #available(iOS 11, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationController?.navigationItem.largeTitleDisplayMode = .always
        } 

You need to put check condition for iOS 11 before building application.

Requirement to test large title:

  • Xcode 9.0,
  • Mac OSX - 10.12.6 or later,
  • iPhone/iPad or Xcode 9 simulator with iOS 11.


回答2:

if (@available(iOS 11.0, *)) {
    [[UINavigationBar appearance] setPrefersLargeTitles:false];
}


回答3:

You can set UILabel into titleView.

UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = YOUR_TITLE_TEXT;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.font = FONT_NAV_BAR;
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;