简体   繁体   中英

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

Try this:

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

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]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM