Reduce the space between bar panel items in a UINavigationBar

I noticed that the empty space between the elements of the bar button is quite large. I want to reduce the space in order to have more space for my title. I tried to create a fixed space and then added it among the buttons, but that didn't work. Does anyone know how to do this?

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; fixedItem.width = 10.0f; self.navigationItem.rightBarButtonItems = @[settingsButtonItem, fixedItem, speakerButtonItem, fixedItem, favouriteButtonItem]; 
+6
source share
2 answers

You are close, but you need a negative fixed space. If your other UIBarButtonItems are using custom views, check your frame for these views. Here's an example of adding two right-hand button elements that hug the edge more. In your case, you also want to add a negative space between the buttons.

 UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpace.width = -8; UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, 44, 44); [backButton setImage:[[UIImage imageNamed:@"ic_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; backButton.imageView.tintColor = [UIColor whiteColor]; [backButton addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside]; self.backNavButton = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.backNavButton.enabled = NO; UIButton *forwardButton = [UIButton buttonWithType:UIButtonTypeCustom]; forwardButton.frame = CGRectMake(0, 0, 44, 44); [forwardButton setImage:[forwardImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; forwardButton.imageView.tintColor = [UIColor whiteColor]; [forwardButton addTarget:self action:@selector(forwardPressed) forControlEvents:UIControlEventTouchUpInside]; self.forwardNavButton = [[UIBarButtonItem alloc] initWithCustomView:forwardButton]; self.forwardNavButton.enabled = NO; self.navigationItem.rightBarButtonItems = @[negativeSpace,self.forwardNavButton,self.backNavButton]; 
+1
source

Using Xcode 6.4, this worked fine for me with 1 line of code!
fooobar.com/questions/64598 / ...

 self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25); 

Note. Unfortunately, this does not move the hit area of ​​the button, but only its image. But a good solution if you just need to move the button a bit!

0
source

Source: https://habr.com/ru/post/970812/


All Articles