Sebastian
Sebastian

Reputation: 108

UIBarButtonItem not shown in subview

I want to create a subview with a NavigationBar and a TableView. In the Navigation Bar should be a button which can edit the tableView. The navigationBar and the TableView is shown in the subview but the barButtonItem not. Is there an error in my code, or is there a problem because it is a subview ?

UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 325, screenSize.size.width, 200)];


                         tableView = [[UITableView alloc] initWithFrame:CGRectMake(-10, 40, screenSize.size.width, 150) style:UITableViewStylePlain];
                         tableView.delegate = self;
                         tableView.dataSource = self;
                         [tagView addSubview:tableView];

                         UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, 45)];
                         self.navigationItem.leftBarButtonItem = self.editButtonItem;
                         [tagView addSubview:navigationBar];

                         [self.view addSubview:tagInstruction];
                         [self.view addSubview:tagView];

Upvotes: 0

Views: 392

Answers (4)

Nicholas Hart
Nicholas Hart

Reputation: 1724

Like Sven said, the self.navigationItem refers to the UINavigationItem for the UINavigationBar which belongs to the view controller's parent UINavigationController. If you want to set your button in the navigation bar you just alloc'd, try this:

UINavigationItem * navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = self.editButtonItem;
[navigationBar setItems:@[navigationItem]];

Upvotes: 0

benjamin.ludwig
benjamin.ludwig

Reputation: 1585

If I need a clean navigationBar I always put my UIViewControllers in a UINavigationController. It does not hurt and you can add navigation functionality later. Then you can use self.navigationItem to add a UIBarButtonItem

Upvotes: 0

Manish Agrawal
Manish Agrawal

Reputation: 11027

self.navigationItem refer to navigationController navigation item and you don't have any navigation controller, you have only custom NavigationBar and hence its not working.
For solving this problem you have to create your own UIButton and add in the subview of NavigationBar.

Upvotes: 2

iNevs
iNevs

Reputation: 99

The navigationItem represents the view controller in a parent’s navigation bar, not in the navigationBar you are constructing here. Put the Button into your navigationBar.

Upvotes: 0

Related Questions