ios - 带有 UIBarButtonItems 的 UIToolBar 等间距,与标题长度无关

标签 ios iphone objective-c autolayout

所以我的任务是向 UIToolBar 添加按钮并将 textView 的 inputAccessoryView 设置到此 UIToolBar。

所以我做了基础:

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"title1" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"longer title2" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"title 3 is a lot longer" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *divider1 = [self barButtonDivider];
UIBarButtonItem *divider2 = [self barButtonDivider];

UIToolBar *toolBar = [UIToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setItems:@[spacer,item1,spacer,divider1,spacer,item2,spacer,divider2,spacer,item3,spacer];
self.textView.inputAccessoryView = toolbar;

所以有更多的方法来解决这个问题。

1) 我可以继承 UIToolBar

2) 我可以将 UIView 子类化并将 View 设置为我需要的大小(superView 的宽度和高度 44),但我需要在自动布局中完成所有这些我尝试过但不能做对。

有没有一种方法可以将上述方法与标准工具栏一起使用,并获得正确的间距,无论我使用多少个按钮,所有按钮的宽度都相等?

预计到达时间:,尝试了以下解决方案但没有成功:

- (UIBarButtonItem *)spacer {

    return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
}

[toolBar setItems:@[[self spacer], item1, [self spacer], divider1, [self spacer], item2, [self spacer], divider2, [self spacer], item3, [self spacer]];

解决了这个解决方案,但我不喜欢它:

 button.width = 150;

按钮将在纵向或横向中保持相同,所以我想这是一个可行的解决方案,您认为如何?

最佳答案

我不确定,但问题可能在于重复使用垫片。每次需要灵活空间时,尝试创建新的 UIBarButtonSystemItemFlexibleSpace

关于ios - 带有 UIBarButtonItems 的 UIToolBar 等间距,与标题长度无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341610/

相关文章:

objective-c - 没有像素化的 CAE 发射单元放大

ios - 自定义表格单元格不显示任何内容 - Swift 2

objective-c - 为什么我在 xcode 控制台中收到此 CPSqliteStatementPerform 错误

iphone - Cocoa-Touch:如何进行布局

iphone - 静态 iOS 库为设备而不是模拟器构建

iphone - 通过 Cocoa Touch 更改 iPhone 应用程序图标

iphone - 使用 AVAudioRecorder 重新录制

ios - 将一个对象与其他几个对象联系起来

ios - 这个 block 会像我期望的那样工作吗?

ios - 使用 iTunes Search API 获取应用关键字