UIViewController Title attribute in Storyboard

后端 未结 10 2103
南旧
南旧 2021-02-03 19:58

I am setting the title field of a UIViewController via Interface Builder/Storyboard: \"enter

相关标签:
10条回答
  • 2021-02-03 20:03

    I think it works as designed although we expect another behaviour. If you print the title property in - (void)viewDidLoad it will be the same value that you set in story board so I see no reason of this not working unless Apple's choice.

    0 讨论(0)
  • 2021-02-03 20:07

    Step 1

    If you're looking at a Xib in Xcode's Interface Builder, take a look in the "Document Outline" panel (second panel from the left). Expand the view controller you're working with until you find an icon labelled: Navigation Item.

    Document Outline panel showing Navigation Item

    Step 2

    If you then highlight the Navigation Item and open up the Utilities panel (the farthest on the right), and click the Attributes Inspector, you'll see where you can set the title of the view controller. This is the way to do it in Interface Builder, rather than doing it through code.

    Utilities panel showing how to set title of view controller

    0 讨论(0)
  • 2021-02-03 20:14

    Everything else on this page failed. For now, this worked, in code, in viewDidLoad:

    NSString* text = @"My page title";
    UIFont* font = [UIFont systemFontOfSize:20.0];
    const CGSize SIZE = [text sizeWithAttributes:@{NSFontAttributeName:font}];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SIZE.width, SIZE.height)];
    label.text = text;
    label.textColor = UIColor.blackColor;
    self.navigationItem.titleView = label;
    
    0 讨论(0)
  • 2021-02-03 20:14

    If you have UINavigationItem present, then you must use the navigation item title in the storyboard. If you don't have a navigation item for a view controller, then the UINavigationController will use the view controller's title property.

    Example :

    In your storyboard, if you have a navigation item the navigation controller doesn't need to read the view controller's title. Even if the title field is empty for this navigation item.

    Remove the navigation item (if you can, you won't be able to do it for the root view controller but you will for the others) and your title will be correctly loaded

    0 讨论(0)
  • 2021-02-03 20:22

    a) If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behavior?

    I believe this is the intended behavior. I think that the purpose of the title attribute of a view controller is more of a property that can be used at the developer's discretion perhaps for distinguishing between controllers.

    Another reason for this change I think is that your navigation item's title may need to be different than the tab bar title, since the tab bar title cannot be nearly as long as the navigation title.

    b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.

    I think I mentioned this in my first paragraph. I think that the title attribute of a controller is a property that the developer can use perhaps for distinguishing between controllers.

    0 讨论(0)
  • 2021-02-03 20:24

    You can set the title of the UINavigationBar in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar.

    Setting the title in code offers some different possibilities.

    self.title = @"Your title"; will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.

    self.navigationItem.title = @"Your title"; will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.

    self.navigationController.title = @"Your title"; will set the title of a UITabBarItem but NOT the UINavigationBar.

    0 讨论(0)
提交回复
热议问题