gpt4 book ai didi

iphone - 将基于自定义 View 的 UIBarButtonItem 放置在没有默认水平填充的导航栏中

转载 作者:IT王子 更新时间:2023-10-29 07:47:10 25 4
gpt4 key购买 nike

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

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

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

由于 UIBarButtonItems 没有我可以访问的“框架”,我无法像普通 UIView 那样将它们定位在它们的 super View 中。

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

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

作为引用,下面是我创建自定义 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;
}

最佳答案

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

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

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

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

注意:这对我在 iOS5 和 iOS6 中有效。鉴于 iOS7 与公开演示有很大不同,那些拥有 iOS7 早期种子的人应该测试一些无意的东西,比如这个 hack,是否会在 iOS6 之后继续为你工作。

关于iphone - 将基于自定义 View 的 UIBarButtonItem 放置在没有默认水平填充的导航栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com