所以我的任务是向 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/