我在编程和迅捷方面非常新手,所以我在YouTube上学习教程。我编写了示例中的代码,但我遇到了一个bug。告诉我怎么才能修好它。在教程中,您应该有一个显示和消失的键盘。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyBoardNotification()
}
deinit {
removeKeyBoardNotification()
}
@IBOutlet weak var scrollViewLogInScreen: UIScrollView!
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
@IBAction func logInButtonTapped(_ sender: UIButton) {
topTextField.resignFirstResponder()
bottomTextField.resignFirstResponder()
}
@objc
This code doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func registerForKeyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(kbWillHide), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
This code also doesn't work with following errors:
Type 'NSNotification.Name' has no member 'UIResponder'
**func removeKeyBoardNotification() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name:
NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}**
func kbWillShow(_ notification: Notification) {
let userInfo = notification.userInfo
let kbFrameSize = (userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
scrollViewLogInScreen.contentOffset = CGPoint.init(x: 0, y: kbFrameSize.height)
}
func kbWillHide() {
scrollViewLogInScreen.contentOffset = CGPoint.zero
}
}
发布于 2020-11-04 17:24:35
只需删除名称中的NSNotification.Name
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
它必须是这样的:
NotificationCenter.default.addObserver(self, selector: #selector(kbWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
https://stackoverflow.com/questions/64677166
复制相似问题