xcode 如何删除 UIBarButtonItem 旁边的填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11814983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:08:21  来源:igfitidea点击:

How to remove padding next to a UIBarButtonItem

xcodeuinavigationbaruibarbuttonitem

提问by carbonr

I have added a custom button to the navigation bar's custom right bar button item as shown in the image.

我在导航栏的自定义右侧栏按钮项中添加了一个自定义按钮,如图所示。

I'm want to be able to remove the space after the button so that it touches the right edge of the screen but couldn't find a solution even after searching. If someone could mention how, would be great.

我希望能够删除按钮后的空间,使其接触屏幕的右边缘,但即使在搜索后也找不到解决方案。如果有人能提到如何,那就太好了。

This is the code I'm using

这是我正在使用的代码

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = aBarButtonItem;

enter image description here

在此处输入图片说明

采纳答案by carbonr

You have to create a new view, add button and everything else you want and add it to the titleView as shown.

您必须创建一个新视图,添加按钮和您想要的所有其他内容,并将其添加到 titleView,如图所示。

- (void)viewDidLoad {
    self.navigationItem.titleView = [self titleView];
}

- (UIView *)titleView {
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat width = 0.95 * self.view.frame.size.width;
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
..
// please add what you need here
....
}

回答by Tom

You can add a fixed space element with a negative width (I know, this sounds like a hack, but it works):

你可以添加一个负宽度的固定空间元素(我知道,这听起来像一个黑客,但它有效):

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -5;

self.navigationItem.rightBarButtonItems = @[spaceFix, aBarButtonItem];

回答by Kyr Dunenkoff

You can't, unless you subclass UINavigationBaror override its methods via category.

你不能,除非你UINavigationBar通过类别子类化或覆盖它的方法。

回答by Bobj-C

You can do this by adding a UIView then add inside it your button with x=spaceWidth y = 0.

您可以通过添加一个 UIView 来做到这一点,然后在其中添加 x=spaceWidth y = 0 的按钮。

look at the below code for the right item (this is inside UINavigationItem category)

查看下面正确项目的代码(这是在 UINavigationItem 类别中)

1 -

1 -

UIView *_rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [_rightView setBackgroundColor:[UIColor purpleColor]];//debug color

UIView *_rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [_rightView setBackgroundColor:[UIColor purpleColor]];//debug color

2-

2-

    UIButton *aLeftButton = [[UIButton alloc] init];

   //...//

    aLeftButton.frame = CGRectMake(-15, 0, 44, 44);//in your case +15
    [aLeftButton setBackgroundImage:[UIImage imageNamed:buttonName] forState:UIControlStateNormal];
    [aLeftButton setBackgroundImage:[UIImage imageNamed:[buttonName stringByAppendingString:@"Selected"]] forState:UIControlStateSelected];
    [aLeftButton setAdjustsImageWhenDisabled:NO];
    [aLeftButton setAdjustsImageWhenHighlighted:NO];

    [aLeftButton addTarget:theSender action:aSelector forControlEvents:UIControlEventTouchUpInside];
    [_rightView addSubview:aLeftButton];
    UIBarButtonItem *aBarButton = [[UIBarButtonItem alloc] initWithCustomView:_rightView];
    [leftBarButtonsItem addObject:aBarButton];`

3

3

self.leftBarButtonItem = leftBarButtonsItem;

self.leftBarButtonItem = leftBarButtonsItem;