ios 在导航栏中放置基于自定义视图的 UIBarButtonItem,没有默认的水平填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4715280/
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-08-30 18:33:40  来源:igfitidea点击:

Placing a custom view based UIBarButtonItem in the navigation bar without default horizontal padding

iphoneiosuinavigationbaruibarbuttonitemuinavigationitem

提问by idStar

How do I remove the horizontal padding to the left and right of custom left and right UINavigationBar items? There seems to be ~ 10 points of padding that iOS sets by default.

如何删除自定义左右 UINavigationBar 项目左右两侧的水平填充?iOS 默认设置了大约 10 个填充点。

I'm customizing left and right navigation bar buttons (I have given up on trying to set my own backButtonItem, so I'm just using the leftBarButtonItem).

我正在自定义左右导航栏按钮(我已经放弃尝试设置自己的 backButtonItem,所以我只使用 leftBarButtonItem)。

In either case (left or right), pressing these custom buttons indicates that Apple seems to preserve some padding to the left of the leftBarButtonItem, and to the right of the rightBarButtonItem; regardless of how wide I make the custom background and image properties of the UIButton I place inside the left/right bar button item as its custom view.

在任何一种情况下(向左或向右),按下这些自定义按钮表明 Apple 似乎保留了一些填充到 leftBarButtonItem 的左侧和 rightBarButtonItem 的右侧;无论我将 UIButton 的自定义背景和图像属性设置为多宽,我都将其放置在左/右栏按钮项中作为其自定义视图。

Since UIBarButtonItems have no "frame" I can access, I can't position them within their superview like I can normal UIViews.

由于 UIBarButtonItems 没有我可以访问的“框架”,我不能像普通 UIViews 一样将它们放置在它们的超级视图中。

Any suggestions on how to remove this default padding? See screen shot attached to see the bit I'm trying to reduce to a zero width. In the screen shot, the plus icon appears shifted to the right because I gave it an inset; but the highlighted background image, also presumably using that inset, is getting clipped on its right side).

有关如何删除此默认填充的任何建议?请参阅附加的屏幕截图以查看我试图减少到零宽度的位。在屏幕截图中,加号图标似乎向右移动,因为我给了它一个插图;但是突出显示的背景图像(也可能使用该插图)在其右侧被剪裁)。

See image at: https://skitch.com/starbaseweb/rj2e5/ios-simulator

见图片:https: //skitch.com/starbaseweb/rj2e5/ios-simulator

For reference, here's how I'm creating my custom UIBarButtonItem (in this case, it's the right button):

作为参考,以下是我创建自定义 UIBarButtonItem 的方法(在本例中,它是正确的按钮):

- (UIBarButtonItem *)customAddButtonItemWithTarget:(id)target action:(SEL)action {
  UIButton *customButtonView = [UIButton buttonWithType:UIButtonTypeCustom];

    customButtonView.frame = CGRectMake(0.0f, 0.0f, 45.0f, 44.0f);

    [customButtonView setBackgroundImage:
        [UIImage imageNamed:@"bgNavBarButton-OutsideRight-Normal.png"] 
        forState:UIControlStateNormal];
    [customButtonView setBackgroundImage:
        [UIImage imageNamed:@"bgNavBarButton-OutsideRight-Highlighted.png"] 
        forState:UIControlStateHighlighted];

    [customButtonView setImage:
        [UIImage imageNamed:@"bgNavBarButton-Add-Normal.png"] 
        forState:UIControlStateNormal];
    [customButtonView setImage:
        [UIImage imageNamed:@"bgNavBarButton-Add-Highlighted.png"] 
        forState:UIControlStateHighlighted];

    [customButtonView addTarget:target action:action 
        forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *customButtonItem = [[[UIBarButtonItem alloc] 
        initWithCustomView:customButtonView] autorelease];
    [customButtonView setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 0.0f)];

    //customButtonItem.imageInsets = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 0.0f);

    return customButtonItem;    
}

回答by idStar

55As commented above, the solution I went with is based on this answer to a different, but very much related question: How to adjust UIToolBar left and right padding. It is also facilitated by (and depends on) iOS5, which allows you to set multiple buttons on the left or right side, instead of just one.

55如上所述,我采用的解决方案是基于对一个不同但非常相关的问题的回答:How to adjust UIToolBar left and right padding。它也由(并取决于)iOS5 提供了便利,它允许您在左侧或右侧设置多个按钮,而不仅仅是一个。

Here's an example of removing the padding to the left of a custom left button item:

这是删除自定义左按钮项左侧的填充的示例:

UIBarButtonItem *backButtonItem // Assume this exists, filled with our custom view

// Create a negative spacer to go to the left of our custom back button, 
// and pull it right to the edge:
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
    target:nil action:nil];
negativeSpacer.width = -5; 
// Note: We use 5 above b/c that's how many pixels of padding iOS seems to add

// Add the two buttons together on the left:
self.navigationItem.leftBarButtonItems = [NSArray 
    arrayWithObjects:negativeSpacer, backButtonItem, nil];

And with this, the left padding for the left bar button item in a navigation bar, is gone!

这样,导航栏中左侧栏按钮项的左侧填充就消失了!

NOTE: This has worked for me in iOS5 and iOS6. Given that iOS7 is considerably different (from the public demos), those of you with the early seeds of iOS7 should test if something so unintentional, like this hack, will actually continue to work for you beyond iOS6.

注意:这在 iOS5 和 iOS6 中对我有用。鉴于 iOS7 有相当大的不同(与公开的演示版),那些拥有 iOS7 早期种子的人应该测试一下,像这次 hack 之类的无意识的东西是否真的会在 iOS6 之后继续为您服务。

回答by Evadne Wu

I have tried this and it works:

我试过这个,它的工作原理:

1) Create a custom UIToolbar subclass, which does nothing in -drawRect:, and is not opaque, and has backgroundColor = [UIColor clearColor].

1) 创建一个自定义的 UIToolbar 子类,它在 -drawRect: 中什么都不做,并且不是不透明的,并且具有 backgroundColor = [UIColor clearColor]。

2) Create a custom UIBarButtonItem with the toolbar as the custom view.

2)创建一个自定义 UIBarButtonItem ,工具栏作为自定义视图。

3) Add your buttons to the custom toolbar.

3) 将您的按钮添加到自定义工具栏。

4) In your custom toolbar override -layoutSubviews and do your own spacing.

4)在您的自定义工具栏中覆盖 -layoutSubviews 并做您自己的间距。