如何替代sizeToFit使键盘inputAccessoryView工具栏按钮均匀分布?
问题:UITextView输入工具栏图标左对齐,无法均匀分布
我参考Paul Hudson的代码,在Objective-C遗留项目中为UITextView的inputAccessoryView添加了UIToolbar。但使用sizeToFit方法后,工具栏上的图标并未均匀分布,而是左对齐。我希望四个图标能像示例那样从左到右均匀排列,以下是我的代码,该代码在ViewDidLoad中调用:
-(void)addToolBarToKeyboardOfTextView: (UITextView*)view { UIImage *addimg = [UIImage imageNamed:@"addimage"]; UIImage *addfile = [UIImage imageNamed:@"addfile"]; UIImage *showtip = [UIImage imageNamed:@"showtip"]; UIImage *close = [UIImage imageNamed:@"close"]; UIToolbar *bar = [[UIToolbar alloc] init]; UIBarButtonItem *addImage = [[UIBarButtonItem alloc] initWithImage:addimg style:UIBarButtonItemStylePlain target:self action:@selector(selectPhoto)]; UIBarButtonItem *attachFile = [[UIBarButtonItem alloc] initWithImage:addfile style:UIBarButtonItemStylePlain target:self action:@selector(selectFile)]; UIBarButtonItem *showTip = [[UIBarButtonItem alloc] initWithImage:showtip style:UIBarButtonItemStylePlain target:self action:@selector(showTip)]; UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithImage:close style:UIBarButtonItemStylePlain target:self action:@selector(closeSelf)]; bar.items = @[addImage,attachFile,showtip,close]; [bar sizeToFit]; view.inputAccessoryView = bar; }
当前inputAccessoryView可正常显示,但图标左对齐而非均匀分布。请问如何实现图标均匀排列?
解决方案
要让工具栏按钮均匀分布,核心是利用弹性空间UIBarButtonItem填充按钮间的空隙,自动均分剩余宽度。具体修改如下:
- 创建
UIBarButtonSystemItemFlexibleSpace类型的弹性空间项,这类项会自动拉伸填充空白区域 - 在功能按钮的首尾、以及每两个按钮之间插入弹性空间
- 修复原代码中的变量名冲突和数组元素错误问题
修改后的完整代码:
-(void)addToolBarToKeyboardOfTextView: (UITextView*)view { UIImage *addimg = [UIImage imageNamed:@"addimage"]; UIImage *addfile = [UIImage imageNamed:@"addfile"]; UIImage *showtipImg = [UIImage imageNamed:@"showtip"]; UIImage *closeImg = [UIImage imageNamed:@"close"]; UIToolbar *bar = [[UIToolbar alloc] init]; UIBarButtonItem *addImage = [[UIBarButtonItem alloc] initWithImage:addimg style:UIBarButtonItemStylePlain target:self action:@selector(selectPhoto)]; UIBarButtonItem *attachFile = [[UIBarButtonItem alloc] initWithImage:addfile style:UIBarButtonItemStylePlain target:self action:@selector(selectFile)]; UIBarButtonItem *showTip = [[UIBarButtonItem alloc] initWithImage:showtipImg style:UIBarButtonItemStylePlain target:self action:@selector(showTip)]; UIBarButtonItem *closeBtn = [[UIBarButtonItem alloc] initWithImage:closeImg style:UIBarButtonItemStylePlain target:self action:@selector(closeSelf)]; // 创建弹性空间 UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; // 组装按钮数组:首尾+按钮间插入弹性空间 bar.items = @[flexibleSpace, addImage, flexibleSpace, attachFile, flexibleSpace, showTip, flexibleSpace, closeBtn, flexibleSpace]; [bar sizeToFit]; view.inputAccessoryView = bar; }
原代码问题说明
- 变量名冲突:原代码中
UIImage *close和UIBarButtonItem *close重名,会引发编译警告,已修改为closeImg和closeBtn - 数组元素错误:原数组中
showtip是UIImage对象,不是UIBarButtonItem,会导致运行崩溃,已替换为showTip按钮对象
修改后,弹性空间会自动均分工具栏的剩余宽度,四个功能按钮就能实现从左到右的均匀排列效果。
内容的提问来源于stack exchange,提问作者user6631314
相关产品推荐
相关产品推荐

