3 回答
TA贡献1911条经验 获得超7个赞
有关以下有关如何使用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: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
}
TA贡献1876条经验 获得超5个赞
Swift 4工作
如下创建扩展名并hideKeyboardWhenTappedAround()在Base视图控制器中调用。
//
// 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 关注
- 834 浏览
添加回答
举报