在导航栏中放置基于UIBarButtonItem的自定义视图,而不使用默认的水平填充 [英] Placing a custom view based UIBarButtonItem in the navigation bar without default horizontal padding

查看:98
本文介绍了在导航栏中放置基于UIBarButtonItem的自定义视图,而不使用默认的水平填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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).

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

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.

由于UIBarButtonItems没有框架我可以访问,我无法将它们放在超级视图中,就像我可以正常使用UIViews。

Since UIBarButtonItems have no "frame" I can access, I can't position them within their superview like I can normal 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).

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

供参考,这里是我是如何创建我的自定义UIBarButtonItem(在这种情况下,它是正确的按钮):

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

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


推荐答案

55如上所述,解决方案我选择的是基于这个答案的另一个但非常相关的问题:如何调整UIToolBar左右填充。 iOS5允许你设置多个按钮,而不只是一个按钮,这也是iOS5的便利。

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.

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

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!

注意:这在iOS5和iOS6中对我有用。鉴于iOS7与公众演示有很大不同,那些拥有iOS7早期种子的人应该测试一下如此无意的东西,比如这个黑客,实际上会继续为你的iOS6以外的东西工作。

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.

这篇关于在导航栏中放置基于UIBarButtonItem的自定义视图,而不使用默认的水平填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆