user2656381
user2656381

Reputation: 145

How to add UIBarbuttonItem at centre on UInavigationbar in iOS

I only add UIBarbuttonitem at left or right. Now i want to add UIbarbuttonitem at centre on uinavigationbar. How can i do that? Thanks much

Upvotes: 1

Views: 1034

Answers (2)

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Try this:

 UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [titleButton setFrame:CGRectMake(0, 0, 170, 35)];
 self.navigationItem.titleView =  [[UIBarButtonItem alloc] initWithCustomView:titleButton];

Upvotes: 2

hgwhittle
hgwhittle

Reputation: 9426

You could add another UIBarButtonItem as a 'spacer' like this:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonTapped)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = 125.0f;

[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:spacer, barButton, nil]];

Upvotes: 1

Related Questions