gpt4 book ai didi

iphone - 如何删除 UIToolbar 中自定义 View 之间的空格?

转载 作者:行者123 更新时间:2023-12-03 18:26:36 25 4
gpt4 key购买 nike

我正在尝试使用自定义图像创建一个包含 5 个按钮的 UIToolbar。我这样做的方法是创建 UIButtonTypeCustom 类型的按钮,然后从中创建 UIBarButtonItems,然后使用 setItems:animated:< 将它们添加到工具栏。但是,这会在图像之间增加空格,导致第五张图像超出工具栏右侧的一半。我如何摆脱这些空间?我已经尝试了我能想到的一切。

非常感谢您的帮助。

以下是一些示例代码,说明我将如何处理此问题:

UIButton *button;
UIBarButtonItem *barButton1,*barButton2;

button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button];


button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"bart_tb.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton2 = [[UIBarButtonItem alloc] initWithCustomView:button];

NSArray *items = [NSArray arrayWithObjects: barButton1, barButton2, nil];
[self.toolbar setItems:items animated:NO];

最佳答案

如果你正在寻找的是布局 UIBarButtonItems 小于默认甚至 0 垂直间距(像我一样),那么购买 UIToolBar 就会自动完成;这是我的推荐:

UIToolBar 私有(private)地布局它的项目。苹果工程师永远不会真正期望开发人员覆盖它。使用固定间距覆盖默认的 10Point 水平间距是不够的。由于 UIToolbar 布局其项目至少相距 10 磅,因此您将这些 UIBarButtonItems 的宽度设置为 -10,以便没有间距,如以下代码所示:

CGFloat toolbarHeight = 44.0;
CGRect toolbarFrame = CGRectMake(0,0, self.view.bounds.size.width, toolbarHeight);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];

UIBarButtonItem* noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem* noSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];

noSpace.width = -10.0;
noSpace1.width = -10.0;

[toolbar setItems:[NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil] autorelease],
noSpace,
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil] autorelease],
noSpace1,
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil] autorelease]
, nil]];

关于iphone - 如何删除 UIToolbar 中自定义 View 之间的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1219973/

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