Reputation: 1717
Trying to add 2 buttons on the left, but having some issues with the spacing. Any solutions? I tried negative spacing but it doesn't work. It is just 2 normal buttons initiated with:
self.navigationItem.leftBarButtonItems = @[self.menuButton, self.scoreLabel];
Thanks for the help
Upvotes: 0
Views: 2181
Reputation: 689
Create a UIBarButtonItem, for example
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
set the width of spacer as
spacer.width = -15.0f;
Add this spacer between your self.menuButton and self.scoreLabel objects as shown below,
self.navigationItem.leftBarButtonItems = @[self.menuButton ,spacer , self.scoreLabel];
Upvotes: 2
Reputation: 1206
Check your score button width by giving it a background color or the button title may be right aligned. Otherwise negative spacing works well.
//Create a bar button with negative spacing
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -15.0f;
//Bar Button 1
UIImage *sideImage = [UIImage imageNamed:@"icon1"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, sideImage.size.width, sideImage.size.height)];
[btnSideMenu addTarget:self action:@selector(btnLeftMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
//Bar Button 2
UIImage *sideImage1 = [UIImage imageNamed:@"icon2"];
UIButton *btnSideMenu1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu1 setImage:sideImage1 forState:UIControlStateNormal];
[btnSideMenu1 setImage:sideImage1 forState:UIControlStateHighlighted];
[btnSideMenu1 setFrame:CGRectMake(0, 0, sideImage1.size.width, sideImage1.size.height)];
[btnSideMenu1 addTarget:self action:@selector(btnLeftMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu1];
//Add all the bar buttons in the leftBarButtonItems Array
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:sideMenuItem1, negativeSpacer, sideMenuItem, nil];
Upvotes: 1