简体   繁体   English

如何删除 NavigationBar 的右空间?

[英]How to remove NavigationBar Right space?

I'm playing with Navigation Bar Buttons I need a button from right edge with 0 space, after google search i found below code我正在玩导航栏按钮我需要一个右边缘的按钮,空间为 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];

But unfortunatly in iOS 13 negativeSpacer not working.但不幸的是,在 iOS 13negativeSpacer 中不起作用。 can anybody suggest me solutions.任何人都可以建议我解决方案。

Attached screenshot here附上截图在这里在此处输入图像描述 Thanks.谢谢。

Let's try this easy way, Let's define the width of space BarButtonItem to be width of self.view.frame.size.width - width of the button .让我们试试这个简单的方法,让我们将空间 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];

Hope this helps!希望这可以帮助!

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

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