我想Done
在iOS右侧的键盘顶部添加按钮,用于textView.请告诉我该怎么做?
我想实现与上述键盘类似的东西
它可以使用故事板完成:
将UITextField添加到UIViewController视图.
在UIViewController的第一级添加UIToolbar
将UIBarButtonItem添加到UIToolbar中.
使用IBOutlet将UItoolbar连接到代码.
使用IBAction将UIBarButtonItem连接到代码(如didClick).
使UITextField将委托给UIViewController.
在didClick函数结束编辑(view.endEditing(true))
在委托函数中,textFieldShouldBeginEditing应为:textField.inputAccessoryView = toolbar并返回true.
希望这有帮助:)
UIToolbar* keyboardToolbar = [[UIToolbar alloc] init]; [keyboardToolbar sizeToFit]; UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(yourTextViewDoneButtonPressed)]; keyboardToolbar.items = @[flexBarButton, doneBarButton]; self.yourTextView.inputAccessoryView = keyboardToolbar;
然后添加yourTextViewDoneButtonPressed方法
-(void)yourTextViewDoneButtonPressed { [self.yourTextView resignFirstResponder]; }
斯威夫特3:
func setDoneOnKeyboard() { let keyboardToolbar = UIToolbar() keyboardToolbar.sizeToFit() let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(InsertStatusVC.dismissKeyboard)) keyboardToolbar.items = [flexBarButton, doneBarButton] self.fullNameTextField.inputAccessoryView = keyboardToolbar } @objc func dismissKeyboard() { view.endEditing(true) }
这个解决方案适用于Swift3/4
将此行添加到项目中作为扩展 您的问题已解决.
extension UITextField{ @IBInspectable var doneAccessory: Bool{ get{ return self.doneAccessory } set (hasDone) { if hasDone{ addDoneButtonOnKeyboard() } } } func addDoneButtonOnKeyboard() { let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) doneToolbar.barStyle = .default let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) let items = [flexSpace, done] doneToolbar.items = items doneToolbar.sizeToFit() self.inputAccessoryView = doneToolbar } @objc func doneButtonAction() { self.resignFirstResponder() } }
在延期之前
后延伸