3 回答

TA貢獻1911條經(jīng)驗 獲得超7個贊
有關(guān)以下有關(guān)如何使用Swift在Xcode 6.1中關(guān)閉鍵盤的問題的答案:
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: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
}

TA貢獻1876條經(jīng)驗 獲得超5個贊
Swift 4工作
如下創(chuàng)建擴展名并hideKeyboardWhenTappedAround()在Base視圖控制器中調(diào)用。
//
// UIViewController+Extension.swift
// Project Name
//
// Created by ABC on 2/3/18.
// Copyright ? 2018 ABC. All rights reserved.
//
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(hideKeyboard))
view.addGestureRecognizer(tapGesture)
}
@objc func hideKeyboard() {
view.endEditing(true)
}
}
- 3 回答
- 0 關(guān)注
- 868 瀏覽
添加回答
舉報