Add items to NavigationBar (Not using UINavigationController)

后端 未结 5 499
北恋
北恋 2020-12-31 13:07

I have a UIViewController with a UITableView in it, and also added a UINavigationBar. How can I add and \"edit\" button and a \"+\" button in that bar programmatically? (I h

5条回答
  •  再見小時候
    2020-12-31 13:37

    Your UIViewController has a navigationItem property. You can set the left and right bar button items with self.navigationItem.leftBarButtonItem = ... and self.navigationItem.rightBarButtonItem = ...

    Edit:

    OK, I assume you have a reference to your UINavigationBar? Then I guess you'd add a single UINavigationItem to it:

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
    theNavigationBar.items = [NSArray arrayWithObject:item];
    [item release]; // or keep this as an instance variable
    

    and then set that item's left and right buttons:

    theNavigationBar.topItem.leftBarButtonItem = ...;
    theNavigationBar.topItem.rightBarButtonItem = ...;
    

    I haven't tried this, but I think it should work.

提交回复
热议问题