繁体   English   中英

Swift 4-如何使用按钮启用/禁用键盘

[英]Swift 4 - How to enable / disable keyboard by using buttons

我棘手的问题是:

我有2个按钮和2个UITextView。 如您所知,当我按下UITextView时,会出现键盘。 好吧,这不是我想要的。

我想根据录音的特定@IBAction启用/禁用显示器的键盘。

该场景如下:-第一个按钮(键盘图标)允许用户显示键盘以在其中一个UITextView中键入内容,其中FirstResponder初始化位于最上面。

  • 第二个按钮(REC图标)允许用户在预选的TextView中讲话和显示,但不显示键盘

我已经知道有:

isUserInteractionEnabled

textViewDidBeginEditing

但这确实不太适合和/或解决我的问题。

这里的屏幕更加清晰(不要介意第三个绿色的验证按钮,它仅用于.POST功能):

在此处输入图片说明

谢谢你的帮助!

如果我正确理解了您的问题,则您不希望在用户点击textField时出现键盘,而仅在用户点击“键盘”按钮时才出现,而在点击其他按钮时不显示键盘。

所有发布的答案大多只集中在显示键盘的第二部分,而在点击按钮时将其关闭。 更棘手的是防止用户点击textField时出现键盘:)

您可以尝试的可能解决方案及其缺点:

解决方案1:

尝试设置textfield isEnabled = false确保这将阻止键盘在用户点击textField时出现,但猜测什么键盘甚至在调用textfield.becomeFirstResponder()也不会出现textfield.becomeFirstResponder()啊,麻烦:)

解决方案2:

实现UITextField委托的textFieldShouldBeginEditing ,并根据使用的是按钮还是textField本身重现true或false。

当然可以,但是您将需要找出方法告诉textFieldShouldBeginEditing为什么由于按钮或因为对textField的触摸而再次触发它的原因。

我的解决方案:

使用2个textFields。 一个将永远禁用用户交互,而使用另一个textField,它将永远不会出现在用户面前,但会在需要时照顾显示键盘。

足够多的讨论让代码:)

第1步:

假设您出现在屏幕上的textField称为textField

textfield.isEnabled = false

这样可以确保您所做的任何键盘都不会出现在此键盘上。

第2步:

创建零帧的临时文本字段(用户永远不能点击:P)

    tempTextField = UITextField(frame: CGRect.zero)
    tempTextField.delegate = self
    self.view.addSubview(tempTextField)

第三步:

现在,当用户点击“显示键盘”按钮时,将您的临时文本字段设置为第一响应者

tempTextField.becomeFirstResponder()

当用户点击其他按钮时, resignFirstResponder的resignFirstResponder。

tempTextField.resignFirstResponder()

第四步:

但是请等待,当用户键入内容时,我的textField上什么也没有出现。 等一下简单实现

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == tempTextField {
        self.textfield.text = (self.textfield.text ?? "") + string
    }
    return true
}

编辑:

您实际上并不需要两个textField,也可以使用UILabelUITextField来实现相同的效果。 我选择UITextField是因为不确定您的其他要求!

问题已解决。 希望能帮助到你 :)

单击按钮时关闭键盘的功能:

@IBAction func hideKeyboard() {
     textView.resignFirstResponder()
 }

使键盘显示的功能:

 @IBAction func showKeyboard() {
     textView.becomeFirstResponder()
 }

这样的事情应该起作用:

class MyViewController: UIViewController, UITextViewDelegate {

    @IBOutlet var yourTextView: UITextView

        override func viewDidLoad() {
            super.viewDidLoad()

            yourTextView.delegate = self
        }

        @IBAction func showKeyboard(_ sender: UIButton) {
        self.yourTextView.becomeFirstResponder()

    }

      @IBAction func hideKeyboard(_ sender: UIButton) {
        self.yourTextView.resignFirstResponder()

    }

    }

你说

但这对我来说不是真的。

为什么?

您可以使用(Swift 3)禁用文本字段交互:

textField.isUserInteractionEnabled = false

接下来,当您单击键盘按钮时,您可以重新启用交互,因此,当用户单击一个文本字段时,键盘将打开。

在键盘上关闭(您可以看到UIKeyboardWillHideNotification回调通知),可以将交互设置为false。

只需为您复制并粘贴简单的代码,即可将内置键盘的附件按钮

 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
}


@IBAction func doneBtnfromKeyboardClicked (sender: Any) {
        self.contentView.endEditing(true)
    }

暂无
暂无

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

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