繁体   English   中英

在 Swift 上编辑 UItextfield 时关闭键盘

[英]Dismiss Keyboard when editing UItextfield on Swift

我想用两个文本字段制作一个计算器。 有数字按钮和数学运算按钮。 但是单击文本字段时我无法关闭键盘。 我从不想显示键盘,但是想用数字按钮编辑文本字段。

touchbegans 或 textFieldShoulBeginEditing 方法对我不起作用。

关于如何使用下面的Swift关闭Xcode 6.1键盘的问题的答案:

import UIKit

class ItemViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textFieldItemName: UITextField!

    @IBOutlet var textFieldQt: UITextField!

    @IBOutlet var textFieldMoreInfo: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldItemName.delegate = self
        textFieldQt.delegate = self
        textFieldMoreInfo.delegate = self
    }

                       ...

    /**
     * Called when 'return' key pressed. return NO to ignore.
     */
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


   /**
    * Called when the user click on the view (outside the UITextField).
    */
   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true)
   }

}

此信息的来源 )。

编辑

这个解决方法是针对上述问题的,因此请停止使用此答案。 这不仅仅是解雇键盘。 这是关于能够使用自定义inputView编辑文本字段,并在没有键盘可见时使文本字段可编辑。

这使您的文本字段键盘消失并仍可编辑。

Objective-C的

yourTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];

迅速

yourTextField.inputView = UIView(frame: CGRectZero)

尝试这个

self.endEditing = YES; 

在您的ViewController中

我从Ezimet的回答中得到了解决方案

对于Swift 5

myTextField.inputView = UIView(frame: CGRect.zero)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM