テキストフィールドに入力でき、画面をスクロールするアプリを作成中です
本を参考にしつつ、写しながら行いました。
メインストーリーボードには、スクロールビュー、ビュー、テキストフィールド、ラベルが配置されています。
本を写しながら、コードを打ち込んだのですが、エラーが出てしまいました。
class ViewController: UIViewController,UITextFieldDelegate { @IBOutlet weak var myScrollView: UIScrollView! @IBOutlet weak var contentView: UIView! @IBOutlet var myTextFields: [UITextField]! func textFieldDidBeginEditing(_ textField: UITextField) { editingField = textField } func textFieldDidEndEditing(_ textField: UITextField) { editingField = nil } var overlap:CGFloat = 0.0 var lastOffsetY:CGFloat = 0.0 @IBAction func tapView(_ sender: Any) { view.endEditing(true) } @objc func keyboardChangeFrame(_ notification : Notification){ guard let fld = editingField else{ return } let userInfo = (notification as Notification).userInfo! let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let fldFrame = view.convert(fld.frame,from:contentView) overlap = fldFrame.maxY - keyboardFrame.minY + 5 if overlap>0{ overlap += myScrollView.contentOffset.y myScrollView.setContentOffset(CGPoint(x:0,y:overlap), animated: true) } } @objc func keyboardWillShow(_ notification: Notification){ lastOffsetY = myScrollView.contentOffset.y } @objc func keyboardDidHide(_ notification: Notification){ myScrollView.setContentOffset(CGPoint(x:0,y:lastOffsetY), animated: true) } override func viewDidLoad() { super.viewDidLoad() myScrollView.keyboardDismissMode = .onDrag // Do any additional setup after loading the view, typically from a nib. let scrollFrame = CGRect(x: 0, y: 20, width: view.frame.width, height: view.frame.height) myScrollView.frame = scrollFrame let contentRect = contentView.bounds myScrollView.contentSize = CGSize(width: contentRect.width, height: contentRect.height) for fld in myTextFields{ fld.delegate = self } let notification = NotificationCenter.default notification.addObserver(self, selector:#selector(ViewController.keyboardChangeFrame(_:)) , name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardDidHide(_:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) } }
これが、実際に打ち込んだコードです。
エラーが出たのはその中の
let notification = NotificationCenter.default notification.addObserver(self, selector:#selector(ViewController.keyboardChangeFrame(_:)) , name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardDidHide(_:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) } }
というコードで、NSNotification.Name.の後の「UIKeyboardDidChanfeFrame」、「UIKeyboardWillShow」、「UIKeyboardDidHide」のところに赤線が引かれて
'UIKeyboardDidHide' has been renamed to 'UIResponder.keyboardDidHideNotificationReplace 'UIKeyboardDidHide' with 'UIResponder.keyboardDidHideNotification'」というエラーが出てしまいます。
これをFixボタンでエラーが以前のために変更したところ、
notification.addObserver(self, selector:#selector(ViewController.keyboardChangeFrame(_:)) , name: NSNotification.Name.UIResponder.keyboardDidChangeFrameNotification, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil) notification.addObserver(self, selector: #selector(ViewController.keyboardDidHide(_:)), name: NSNotification.Name.UIResponder.keyboardDidHideNotification, object: nil)
になり、また別のエラーが出てしまいました。
「Type 'NSNotification.Name' has no member 'UIResponder'」
翻訳したところ「タイプ 'NSNotification.Name'にはメンバー 'UIResponder'がありません」と出てしまいました。ネットで検索などしたのですが、力不足で見つけれませんでした。
エラーを修正するには何を入れたらいいのか、解決するためにどのように調べればいいのかを教えてい下さい
解決方法がありましたら、ご助言お願いします。
回答1件