UINavigationBar - Set title programmatically?

2019-02-01 03:01发布

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 clause in the ViewDidLoad method, so if x { change the title }.

I have tried self.title = @"title", but this changes the title of the tab bar item itself.

So, how is this done?

10条回答
仙女界的扛把子
2楼-- · 2019-02-01 03:54

Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!

查看更多
做个烂人
3楼-- · 2019-02-01 03:55

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

    [self setTitle:@"My Title"];
查看更多
The star\"
4楼-- · 2019-02-01 04:01

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
查看更多
在下西门庆
5楼-- · 2019-02-01 04:02

In ViewDidLoad of your ViewController.m just write,

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

Here is the Output:

enter image description here

查看更多
登录 后发表回答