2017-08-02 126 views
0

我有两个UITextFields和数字键盘UIButton在同一个窗体上。我不希望用户使用默认键盘,但使用我的UIButton将值输入到它。但每次点击textField时,都会显示默认键盘。Swift 3禁用或隐藏键盘UITextField

我试图将isUserInteractionEnabled设置为false,键盘没有显示,但动作选择器功能没有触发touchdown事件,这就是为什么我无法更改当前UITextField背景颜色。

请帮忙!!!

+0

isUserInteractionEnabled是假的的UITextField和触下承担的UIButton我是rightt? –

+0

我希望touchdown事件触发UITextField上的选择器函数,但是当isUserInteractionEnabled为false时,触发未触发 –

+0

您应该启用isUserInteractionEnabled,然后才可以轻击工作。在触摸下请添加self.view.endEditing(true) –

回答

0

为UITextField isUserInteractionEnabled真实,让

settings.addTarget(self, action: #selector(touched), for: .touchUp) 

func touched(sender: UIButton!) { 
    self.view.endEditing(true) 
    // change background here 
    } 

您也可以尝试resignFirstResponder()隐藏键盘。

+0

我无法更改背景,但如果我从TouchUpInside更改为TouchDown,则会执行endEditing(true)下的代码,但键盘仍显示 –

0

试试这个代码斯威夫特3.

import UIKit 


class ViewController: UIViewController, UITextFieldDelegate { 

    @IBOutlet weak var myTextField: UITextField! 


    override func viewDidLoad() 
    { 
    self.myTextField.delegate = self 
    } 

    @IBAction func Button2Pressed(_ sender: Any) 
    { 
    self.myTextField.text = "one" 
    } 

    @IBAction func Button1pressed(_ sender: Any) 
    { 
    self.myTextField.text = "two" 
    } 

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
    { 
    return false 
    } 

    override func didReceiveMemoryWarning() 
    { 
    super.didReceiveMemoryWarning() 
    } 
}