我在我的應用程序中有十進制墊設置,但我該如何解僱這個鍵盤?我如何獲得完成或返回鍵?我試過UIReturnKeyType.Done
,但沒有顯示任何東西。如何在Swift中的小數點上添加返回鍵?
類似的東西
我在我的應用程序中有十進制墊設置,但我該如何解僱這個鍵盤?我如何獲得完成或返回鍵?我試過UIReturnKeyType.Done
,但沒有顯示任何東西。如何在Swift中的小數點上添加返回鍵?
類似的東西
class ViewController: UIViewController {
let textField = UITextField(frame: CGRect(x: 0, y: 20, width: 200, height: 60))
override func viewDidLoad() {
super.viewDidLoad()
textField.borderStyle = .Line
textField.inputAccessoryView = accessoryView()
textField.inputAccessoryView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
view.addSubview(textField)
}
func accessoryView() -> UIView {
let view = UIView()
view.backgroundColor = UIColor.redColor()
let doneButton = UIButton()
doneButton.frame = CGRect(x: self.view.frame.width - 80, y: 7, width: 60, height: 30)
doneButton.backgroundColor = UIColor.greenColor()
doneButton.setTitle("done", forState: .Normal)
doneButton.addTarget(self, action: #selector(ViewController.doneAction), forControlEvents: .TouchUpInside)
view.addSubview(doneButton)
return view
}
@objc func doneAction() {
textField.resignFirstResponder()
}
}
甚至沒有提到我的問題.. –
是的,因爲我無法解決這個問題。我用另一種方式。 – maquannene
@AmitNivedanKalra你可能應該解釋你在這裏做了什麼,如果你正在給一個替代解決方案... – penatheboss
該解決方案爲我=)
來源:https://gist.github.com/jplazcano87/8b5d3bc89c3578e45c3e
我爲UI的擴展TextField,現在我可以在我想要的所有鍵盤上有一個「完成欄」。
extension UITextField{
func addDoneButtonToKeyboard(myAction:Selector?){
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
doneToolbar.barStyle = UIBarStyle.default
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: myAction)
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
}
MyViewController
class myViewController:UIViewController,UITextFieldDelegate{
//MARK: - Outlets
@IBOutlet weak var myTextField: UITextField!
//MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
//You can specify your own selector to be send in "myAction"
myTextField.addDoneButtonToKeyboard(myAction: #selector(self.myTextField.resignFirstResponder))
}
}
這是迄今爲止我所見過的最新解決方案! – user1700737
這裏是一個夫特3使用擴展溶液。如果您的應用中有多個數字UITextField
對象,那麼這是理想的選擇,因爲它可以靈活地爲每個UITextField
決定是否執行自定義操作完成或取消被點擊。使用自定義
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet { myNumericTextField?.addDoneCancelToolbar() }
}
使用示例完成行動:
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet {
myNumericTextField?.addDoneCancelToolbar(onDone: (target: self, action: #selector(doneButtonTappedForMyNumericTextField)))
}
}
func doneButtonTappedForMyNumericTextField() {
print("Done");
myNumericTextField.resignFirstResponder()
}
在iPad上,你可以添加
使用默認的操作使用示例它到了鍵盤頂部的酒吧,但在其他地方設備您的選擇是有限的。我會添加一個按鈕出現在它的頂部或建立自定義鍵盤 – Sethmr