【问题标题】:How to remove NavigationBar Right space?如何删除 NavigationBar 的右空间?
【发布时间】:2020-08-15 14:43:48
【问题描述】:

我正在玩导航栏按钮,我需要一个右边缘的按钮,空间为 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 不起作用。任何人都可以建议我解决方案。

这里附上截图 谢谢。

【问题讨论】:

  • 你能分享一下它的截图吗?
  • 请查看附件图片
  • 所以你不想要按钮右侧的空白区域吗?
  • 是的,我想删除空格
  • 尝试使用图像纵横比模式作为填充

标签: ios objective-c uinavigationcontroller uiedgeinsets


【解决方案1】:

让我们试试这个简单的方法,让我们将空间 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];

希望这会有所帮助!

【讨论】:

  • 尝试使用负值,例如 '-(self.view.frame.size.width - 130)' 仍然在同一个地方,就像有问题的附加图片一样
猜你喜欢
  • 1970-01-01
  • 2018-01-16
  • 2012-02-22
  • 2011-08-28
  • 2021-11-02
  • 2023-03-26
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
相关资源
最近更新 更多