Reduce left space and right space from left and right panel navigation bar

I add two custom buttons on the navigation bar. one on the left and the other on the right. I successfully dealt with this,

but I was not able to reduce the space between the starting point and the frame of the buttons.

I tried a lot, also gave a negative value of x, still did not help. Here is the code for the buttons

-(void)addLeftButton { UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; aButton.backgroundColor = [UIColor redColor]; [aButton setTitle:@"H" forState:UIControlStateNormal]; aButton.frame = CGRectMake(0.0, 0.0, 40, 40); [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; [aButton addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setLeftBarButtonItem:aBarButtonItem]; } -(void)addRightButton { UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; aButton.backgroundColor = [UIColor greenColor]; [aButton setTitle:@"B" forState:UIControlStateNormal]; aButton.frame = CGRectMake(0.0, 0.0, 40, 40); [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; [aButton addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setRightBarButtonItem:aBarButtonItem]; } 

Also tried using

 aBarButtonItem.width = -16; 

but it didn’t help.

How to achieve this ...? Thanks in advance...

Here are some links that I preferred but didn't help much How to edit a blank space on the left, on the right UIBarButtonItem in UINavigationBar [iOS 7]

How to edit blank space on left, right UIBarButtonItem in UINavigationBar [iOS 7]

enter image description here

+1
ios xcode navigationbar
source share
2 answers
 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -16; [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,yourbutton, nil] animated:YES]; 

Use this.

+4
source share

Here is the Swift version:

 let negativeSpacer = UIBarButtonItem() negativeSpacer.width = -16 let hamburgerButton = UIBarButtonItem(image: UIImage(named: "hamburgerMenu")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(menuButtonPressed)) navigationItem.setLeftBarButtonItems([negativeSpacer, hamburgerButton], animated: true) 
0
source share

All Articles