iOS 输入附件视图(工具栏)
示例
在键盘上方添加附件视图。这通常用于添加下一个/上一个按钮,或其他按钮,例如“完成/提交”(尤其是对于没有内置返回键的数字/电话/小数键盘类型)。
迅速
let textField = UITextField() //但是初始化 let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 0) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) let doneButton = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: Selector("done")) let items = [flexibleSpace, doneButton] //将完成按钮推到右侧 toolbar.setItems(items, animated: false) //ortoolbar.items=... toolbar.sizeToFit() textField.inputAccessoryView = toolbar
目标C
UITextField *textField = [[UITextField alloc] init]; UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)]; NSArray *items = @[ flexibleSpace, doneButton ]; [toolbar setItems:items]; [toolbar sizeToFit]; textField.inputAccessoryView = toolbar;