NavigationItem setRightBarButtonItems too wide

enter image description here

I am trying to reduce the distance between these two button elements.

I use

navigationItem setRightBarButtonItems 

to set two button elements, but they are too far apart.

I tried to add a negative space, I tried to add a spacer after it, a fixed space, a flexible space. You don't see anything in the docs that say you cannot change the interval, but I cannot find it.

Thanks for the help in advance.

CHANGE AFTER ANSWER:

Siu Chung Chan's answer was absolutely correct, but since I did not receive it at first, I thought I would share a code that made me realize that he was right.

If you placed all this in one block, it would look like his (very correct) answer:

 UIView *filterBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; UIButton *filterBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; [filterBtn addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside]; [filterBtn setBackgroundImage:[UIImage imageNamed:@"someicon"] forState:UIControlStateNormal]; [filterBtnView addSubview:filterBtn]; UIBarButtonItem *btnFilter = [[UIBarButtonItem alloc] initWithCustomView:filterBtnView]; UIView *selectBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; [selectBtn setBackgroundImage:[UIImage imageNamed:@"someothericon"] forState:UIControlStateNormal]; [selectBtn addTarget:self action:@selector(someOtherMethod:) forControlEvents:UIControlEventTouchUpInside]; [selectBtnView addSubview:selectBtn]; UIBarButtonItem *btnSelect = [[UIBarButtonItem alloc] initWithCustomView:selectBtnView]; [self.navigationItem setRightBarButtonItems:@[btnFilter, btnSelect] animated:YES]; 

For me, the beauty of this is that it looks at how some of the opinions are actually set by Apple to be biased about how they wanted to use them only. Therefore, if you want to create a highly customizable user interface, you need to do a lot of manipulation with UIView to get around their (possibly) unintended barriers.

The moral of the story: if the view is not suitable for you, try to recreate the view from the UIView level and then add it to the view that you want to display.

Thanks again Siu Chung Chan!

+6
source share
3 answers

I have done this before.

You need to create your own UIView for the button. call uibarbuttonitem by default with some left and right padding.

 ViewIconBtn* searchViewIconBtn = [[ViewIconBtn alloc] initWithImage:[UIImage imageNamed:@"searchIcon.png"]]; [searchViewIconBtn.btn addTarget:self action:@selector(toSearch) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem* btnSearch = [[UIBarButtonItem alloc] initWithCustomView:searchViewIconBtn]; UIBarButtonItem *space15 = [NegativeSpacer negativeSpacerWithWidth:15]; [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:space15,btnWishList,btnPost,btnSearch, nil]]; 

btnWishList, btnPost, btnSearch is the ViewIconBtn class. In my project, I created 3 navigation buttons on the right side.

The UIBarButtonItem15 space is used to adjust the indentation between the border and the rightmost bar.

+3
source

The solution you provided works great. If you want to simplify it, you can actually use UIButton as a custom view directly without inserting it into the UIView .

I found that the distance between my new buttons is actually slightly smaller than the standard distance between the apples, so I used your example with UIButton built into UIView , but changed the UIView frame to a bit more.

 UIView *filterBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 35)]; 
0
source
 - (void)addTwoRightBarButtonItems { UIButton *reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom]; reloadBtn.frame = CGRectMake(0.0, 0.0, 45.0, 44.0); [reloadBtn setImage:[UIImage imageNamed:@"reload_icon"] forState:UIControlStateNormal];men reloadBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0); // reloadBtn.backgroundColor = [UIColor redColor]; UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeSystem]; menuBtn.frame = CGRectMake(45.0, 0.0, 45.0, 44.0); UIImage *image = [[UIImage imageNamed:@"menu_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [menuBtn setImage:image forState:UIControlStateNormal]; //menuBtn.backgroundColor = [UIColor greenColor]; UILabel *badgeLbl = [[UILabel alloc]initWithFrame:CGRectMake(25, 8, 18, 18)]; badgeLbl.layer.cornerRadius = 9; [badgeLbl.layer setMasksToBounds:YES]; badgeLbl.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:197.0/255.0 blue:0.0 alpha:1.0]; badgeLbl.textColor = [UIColor colorWithRed:136.0/255.0 green:94.0/255.0 blue:16.0/255.0 alpha:1.0]; badgeLbl.font = [UIFont fontWithName:@"Lato-Bold" size:9.f]; badgeLbl.textAlignment = NSTextAlignmentCenter; badgeLbl.hidden = YES; [menuBtn addSubview:_lblBadge]; UIBarButtonItem *offset = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; offset.width = -10.0; UIView *v = [[UIView alloc]initWithFrame:(CGRect){.size.width = 90.0,.size.height = 44.0}]; [v addSubview:reloadBtn]; [v addSubview:menuBtn]; UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc] initWithCustomView:v]; [self.navigationItem setRightBarButtonItems:@[offset,reloadItem]]; // } 

enter image description here

0
source

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


All Articles