如何删除NavigationBar右边的空间?

问题描述 投票:0回答:1

[我正在使用导航栏按钮,在Google搜索后,我发现下面的代码,我需要从右边开始有0个空格的按钮

 self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:@"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:@selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:self.filterButton];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -25;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];

但是很遗憾,在iOS 13 negativeSpacer中无法正常工作。有人可以向我建议解决方案。

在此处附有屏幕截图enter image description here谢谢。

ios objective-c uinavigationcontroller uiedgeinsets
1个回答
0
投票

让我们尝试这种简单的方法,让我们将BarButtonItem的宽度定义为self.view.frame.size.width - width of the button的宽度。

self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:@"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:@selector(filterAction:) 
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] 
initWithCustomView:self.filterButton];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil 
action:nil];
negativeSpacer.width = self.view.frame.size.width - 45; // As 45 is the frame width of your filter button 
[self.navigationItem setRightBarButtonItems:[NSArray 
arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.