繁体   English   中英

如何删除 NavigationBar 的右空间?

[英]How to remove NavigationBar Right space?

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

附上截图在这里在此处输入图像描述 谢谢。

让我们试试这个简单的方法,让我们将空间 BarButtonItem 的宽度定义为 self.view.frame.size.width 的宽度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];

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM