UINavigationBar - Set title programmatically?

前端 未结 11 1227
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 04:24

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a cl

相关标签:
11条回答
  • 2020-12-24 05:12

    Use this :

        CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
        UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];
    
        UINavigationItem *navItem = [UINavigationItem alloc];
        navItem.title = @"Your Title";
    
        [navBar pushNavigationItem:navItem animated:false];
    
    [self.view addSubView:navBar]; 
    

    or

    self.tableView.tableHeaderView = navBar; etc
    
    0 讨论(0)
  • 2020-12-24 05:14

    If the class is a type of UIViewController, then you can set title as given in the viewDidLoad method.

        [self setTitle:@"My Title"];
    
    0 讨论(0)
  • 2020-12-24 05:15

    I've set the title programmatically using code something like this:

    navBar.topItem.title = @"title";
    

    where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

    If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

    0 讨论(0)
  • 2020-12-24 05:15

    Try this,

    self.navigationItem.title = @"Your title";
    
    0 讨论(0)
  • 2020-12-24 05:16

    In ViewDidLoad of your ViewController.m just write,

    self.navigationItem.title=@"Hello World";
    

    Here is the Output:

    enter image description here

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