前提・実現したいこと
いつもお世話になっております。
キーボードを表示させた際に配置されているobjetをキーボードで隠さないようにしたいです。
調べたところ、上記の問題であれば解決案を説明しているサイトも多くあったため、下記のような挙動が実現できました。
しかしながら文字を入力しようとした際に、y軸調整されていたviewが再び一番下まで戻ってしまう問題が発生しております。
このような場合、どのようなコードを追加すれば問題解決に至るか教えていただけましたら幸いです。
お手数おかけしますが、どうか宜しくお願いします。
該当のソースコード
class PostedScreen: UIViewController { let screenSize = UIScreen.main.bounds.size @IBOutlet weak var keyboardParts: UIView! @IBOutlet weak var postTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() // postTextField.becomeFirstResponder() NotificationCenter.default.addObserver(self, selector: #selector(PostedScreen.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(PostedScreen.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) } @objc func keyboardWillShow(_ notification: NSNotification){ let keyboardHeight = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.height keyboardParts.frame.origin.y = screenSize.height - keyboardHeight - keyboardParts.frame.height } @objc func keyboardWillHide(_ notification: NSNotification){ keyboardParts.frame.origin.y = screenSize.height - keyboardParts.frame.height }
補足情報(FW/ツールのバージョンなど)
xcode9.2
回答1件
あなたの回答
tips
プレビュー