使用 uiTextView 关闭键盘

问题描述 投票:0回答:6

我确信这并不那么困难,但我很难找到有关如何使用文本视图而不是文本字段通过返回/完成键关闭键盘的信息。这是我到目前为止所尝试过的(适用于文本字段。)

提前非常感谢您的帮助!

//  PostTravelQuestion.swift

class PostTravelQuestion: UIViewController, UITextViewDelegate {

    @IBAction func closepostpage(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }


    @IBOutlet var postquestion: UITextView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        postquestion.delegate = self
    }

    self addDoneToolBarToKeyboard:self.textView


    /*func textViewShouldEndEditing(textView: UITextView) -> Bool {

        textView.resignFirstResponder()

        return true
    }*/

    /*override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        postquestion.resignFirstResponder()
        self.view.endEditing(true)
    }*/



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func textViewShouldReturn(textView: UITextView!) -> Bool {
        self.view.endEditing(true);
        return true;
    }
}
swift uitextview xcode6 uikeyboard
6个回答
194
投票

这对我有用:

import UIKit

class ViewController: UIViewController, UITextViewDelegate {


    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self
    }

    /* Updated for Swift 4 */
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n") {
            textView.resignFirstResponder()
            return false
        }
        return true
    }

    /* Older versions of Swift */
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n") {
            textView.resignFirstResponder()
            return false
        }
        return true
    }

}

55
投票

UITextViewDelegate
添加到您的类中,然后在 viewDidLoad 中为您的 textView 或 textField 设置委托。应该看起来像这样:

// in viewDidLoad
textField.delegate = self
textView.delegate = self

斯威夫特3

// hides text views
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if (text == "\n") {
        textView.resignFirstResponder()
        return false
    }
    return true
}
// hides text fields
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (string == "\n") {
        textField.resignFirstResponder()
        return false
    }
    return true
}

斯威夫特2.0

以下语法已针对 Swift 1.2 和 Swift 2.0 进行了测试

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        textView.resignFirstResponder()
        return false
    }
    return true
}

19
投票

当单击

UITextView
上的返回/完成键时,下面的代码将关闭键盘。

在 Swift 3.0 中

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

@IBOutlet var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    textView.delegate = self
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
    if(text == "\n")
    {
        view.endEditing(true)
        return false
    }
    else
    {
        return true
    }
}

在 Swift 2.2 中

func textView(textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
    if text == "\n"
    {
        view.endEditing(true)
        return false
    }
    else
    {
        return true
    }
}

4
投票

使用 UITextView 扩展执行此操作的最简单和最佳方法。
信用: http://www.swiftdevcenter.com/uitextview-dismiss-keyboard-swift/
你的 ViewController 类

class ViewController: UIViewController {

    @IBOutlet weak var myTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 1
        self.myTextView.addDoneButton(title: "Done", target: self, selector: #selector(tapDone(sender:)))
    }
    // 2
    @objc func tapDone(sender: Any) {
        self.view.endEditing(true)
    }
}

添加UITextView扩展

extension UITextView {

    func addDoneButton(title: String, target: Any, selector: Selector) {

        let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                              y: 0.0,
                                              width: UIScreen.main.bounds.size.width,
                                              height: 44.0))//1
        let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)//2
        let barButton = UIBarButtonItem(title: title, style: .plain, target: target, action: selector)//3
        toolBar.setItems([flexible, barButton], animated: false)//4
        self.inputAccessoryView = toolBar//5
    }
}

了解更多详细信息:访问完整文档


4
投票

要隐藏 swift 4 中文本框或文本视图之外任何部分的键盘触摸,请在 ViewController 类中使用这段代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    view.endEditing(true)
    super.touchesBegan(touches, with: event)
}

问候


0
投票

基于其他人的答案(荣誉!),这是我的简约看法:

import UIKit

extension UITextView {

    func withDoneButton(toolBarHeight: CGFloat = 44) {
        guard UIDevice.current.userInterfaceIdiom == .phone else {
            print("Adding Done button to the keyboard makes sense only on iPhones")
            return
        }
        
        let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: toolBarHeight))
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(endEditing))
        
        toolBar.setItems([flexibleSpace, doneButton], animated: false)
        
        inputAccessoryView = toolBar
    }

}

用途:

override func viewDidLoad() {
         super.viewDidLoad()

         textView.withDoneButton()
}
© www.soinside.com 2019 - 2024. All rights reserved.