如何在Stack Exchange应用程序中添加键盘上方的按钮?当你长按UITextView中的文字时如何添加"选择"和"全选"?
答案 0 :(得分:7)
第一个问题,您可以将textField
的{{1}}设置为自定义视图,这可以自定义键盘的标题。
结果:
你可以像下面这样做;
首先,您应该在键盘上方添加要添加的视图。
inputAccessoryView
在class ViewController : UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.inputAccessoryView = Bundle.main.loadNibNamed("CustomAccessoryView", owner: self, options: nil)?.first as! UIView?
中,您可以设置按钮的操作:
CustomAccessoryView
答案 1 :(得分:5)
我建议您为toolbar
的accessoryView属性创建UITextField
。
我们的想法是在文本字段第一次显示之前添加此toolbar
一次。因此,我们将delegate
分配给自己,并使用我们的实施覆盖textFieldShouldBeginEditing
委托调用,以添加accessoryView
。
这是一个简单的例子,你如何实现它:
import UIKit
class ViewController: UIViewController {
// your `UITextfield` instance
// Don't forget to attach it from the IB or create it programmaticly
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Assign the delegate to self
textField.delegate = self
}
}
// MARK: Create extension to conform to UITextfieldDelegate
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
setupTextFieldsAccessoryView()
return true
}
func setupTextFieldsAccessoryView() {
guard textField.inputAccessoryView == nil else {
print("textfields accessory view already set up")
return
}
// Create toolBar
let toolBar: UIToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
toolBar.barStyle = UIBarStyle.black
toolBar.isTranslucent = false
// Add buttons as `UIBarButtonItem` to toolbar
// First add some space to the left hand side, so your button is not on the edge of the screen
let flexsibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) // flexible space to add left end side
// Create your first visible button
let doneButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(didPressDoneButton))
// Note, that we declared the `didPressDoneButton` to be called, when Done button has been pressed
toolBar.items = [flexsibleSpace, doneButton]
// Assing toolbar as inputAccessoryView
textField.inputAccessoryView = toolBar
}
func didPressDoneButton(button: UIButton) {
// Button has been pressed
// Process the containment of the textfield or whatever
// Hide keyboard
textField.resignFirstResponder()
}
}
这应该是你的输出:
答案 2 :(得分:1)
只需复制并粘贴简单代码即可为您嵌入键盘的附件按钮
func addKeyboardToolbar() {
let ViewForDoneButtonOnKeyboard = UIToolbar()
ViewForDoneButtonOnKeyboard.sizeToFit()
let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named: "login-logo"), for: UIControlState.normal)
button.addTarget(self, action:#selector(doneBtnfromKeyboardClicked), for:.touchUpInside)
button.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.width, height: 30) //CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem.init(customView: button)
ViewForDoneButtonOnKeyboard.items = [barButton]
postTextView.inputAccessoryView = ViewForDoneButtonOnKeyboard
}
func doneBtnfromKeyboardClicked (){
self.contentView.endEditing(true)
}
答案 3 :(得分:1)
您必须使用文本字段的inputAccessoryView。
您可以将以下代码段放在viewDidLoad()中:
override func viewDidLoad() {
super.viewDidLoad()
let yourButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 60))
yourButton.backgroundColor = UIColor.blue
yourButton.setTitle("NEXT", for: .normal)
yourButton.setTitleColor(UIColor.white, for: .normal)
yourButton.addTarget(self, action: #selector(self. yourButton), for: .touchUpInside)
NumtextField.inputAccessoryView = yourButton
}
@objc func nextButton()
{
print("do something")
}
答案 4 :(得分:0)
添加一个带有完成按钮的工具栏,该按钮可以取消UITextField上方的键盘,您可以使用以下函数编写UITextField扩展名:
public func addAccessoryView() {
let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "resignFirstResponder")
let flexSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.Default
toolbar.translucent = true
toolbar.tintColor = Color.blue
toolbar.sizeToFit()
toolbar.setItems([flexSpace, doneButton], animated: false)
toolbar.userInteractionEnabled = true
self.inputAccessoryView = toolbar
}
然后您可以在文本字段中调用此函数,如下所示:
textfield.addAccessoryView()