如何移除UIBarButtonItem旁边的填充

7
我已经按照图片所示,在导航栏的右边自定义按钮上添加了一个自定义按钮。 我想要去掉按钮后面的空格,让它与屏幕的右边缘相接触,但即使搜索了也找不到解决方案。如果有人能提及如何解决,那就太好了。
这是我正在使用的代码:
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

4个回答

17

您可以使用负宽度添加一个固定的空间元素(虽然听起来像是一种技巧,但它确实有效):

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];

很遗憾,在Xcode 5的Interface Builder中似乎无法设置负宽度。 - TalkLittle
这个完美地解决了问题。当我添加一个带有自定义视图的右侧UIBarButtonItem时,它留下了一个奇怪的空间...我无法确定原因。 - Moisés Olmedo
有没有办法利用负空间来减少按钮之间的间距?如果我在它们之间放置另一个按钮,它们似乎不会受到影响。 - Claus

3

您需要创建一个新视图,添加按钮和其他所需内容,并将其添加到标题视图中,如下所示。

- (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
....
}

如果我想要扩大可点击区域,我会使用另一个按钮作为容器视图。 - seenickcode

0

您可以通过添加一个UIView,然后在其中添加x=spaceWidth y=0的按钮来实现此操作。

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

1 -

UIView *_rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [_rightView setBackgroundColor:[UIColor purpleColor]];//调试颜色

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

self.leftBarButtonItem = leftBarButtonsItem;

这段代码是关于编程的,意思是将左侧按钮项设置为leftBarButtonsItem。

0

除非你通过子类化 UINavigationBar 或者通过分类重写它的方法,否则你无法这样做。


你能解释一下我如何在子类化或使用分类后进行操作吗?我知道如何做这两个,但不知道如何做这个。 - carbonr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接