質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

975閲覧

スクロールビューにてボタンが反応しません

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/02/10 06:55

キーボードを上に持ち上げたくて、スクロールビュー上でTableViewとUIViewを設置したのですが、なぜかボタンが反応しなくなってしまいました。いろいろ調べてところ、addSubViewはしているし、画面外からもはみ出てはいないです。
もう、訳が分からないので、もし分かる方がいたら対処法を教えて欲しいです。
どうかお願いします。
イメージ説明
イメージ説明

class CommentViewController: UIViewController { var selectedTextField:UITextField? @IBOutlet weak var commentTextField: UITextField! @IBOutlet weak var sendButton: UIButton! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var constraintToButton: NSLayoutConstraint! @IBOutlet weak var commentofscrollView: UIScrollView! let postId = "-M-Ju8lDeVTA7Sqn4_7d" var comments = [Comment]() var users = [User]() override func viewDidLoad() { super.viewDidLoad() commentofscrollView.delegate = self as? UIScrollViewDelegate tableView.dataSource = self tableView.estimatedRowHeight = 80 // tableView.rowHeight = UITableViewAutomaticDimension empty() handleTextField() loadComments() self.textFieldInit() // TextFieldのセットアップ NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_ :)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_ :)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) // ビューに追加 self.view.addSubview(commentofscrollView) } // UIView.animate(withDuration: 0.5) { // self.view.layoutIfNeeded() // self.constraintToButton.constant = keyboardFrame!.height // } } } @objc func textFieldDidChange() { if let commentText = commentTextField.text, !commentText.isEmpty{ sendButton.setTitleColor(UIColor.black, for: UIControlState.normal) sendButton.isEnabled = true return } sendButton.setTitleColor(UIColor.lightGray, for: UIControlState.normal) sendButton.isEnabled = false } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // キーボードイベントの監視開始 NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // キーボードイベントの監視解除 NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } @IBAction func sendButton_TouchUpInside(_ sender: Any) { let ref = Database.database().reference() let commentReference = ref.child("comments") let newCommentId = commentReference.childByAutoId().key let newCommentReference = commentReference.child(newCommentId!) guard let currentUser = Auth.auth().currentUser else { return } let currentUserId = currentUser.uid newCommentReference.setValue(["uid": currentUserId,"commentText": commentTextField.text!], withCompletionBlock: { (error, ref) in if error != nil { ProgressHUD.showError(error!.localizedDescription) return } // let postId = "-M-Ju8lDeVTA7Sqn4_7d" let postCommentRef = ref.child("post-comments").child(self.postId).child(newCommentId!) postCommentRef.setValue(true, withCompletionBlock: { (error, ref) in if error != nil { ProgressHUD.showError(error!.localizedDescription) return } }) self.empty() }) } } extension CommentViewController: UITableViewDataSource,UITextFieldDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return comments.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentTableViewCell let comment = comments[indexPath.row] let user = users[indexPath.row] cell.comment = comment cell.user = user return cell } func textFieldInit() { // 最初に選択されているTextFieldをセット self.selectedTextField = self.commentTextField // 各TextFieldのdelegate 色んなイベントが飛んでくるようになる self.commentTextField.delegate = self } // キーボードが表示された時に呼ばれる @objc func keyboardWillShown(notification: NSNotification) { if let userInfo = notification.userInfo { if let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue, let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue { restoreScrollViewSize() let convertedKeyboardFrame = commentofscrollView.convert(keyboardFrame, from: nil) // 現在選択中のTextFieldの下部Y座標とキーボードの高さから、スクロール量を決定 let offsetY: CGFloat = self.selectedTextField!.frame.maxY - convertedKeyboardFrame.minY if offsetY < 0 { return } updateScrollViewSize(moveSize: offsetY, duration: animationDuration) } } } // キーボードが閉じられた時に呼ばれる @objc func keyboardWillHidden(notification: NSNotification) { restoreScrollViewSize() } // TextFieldが選択された時 func textFieldDidBeginEditing(_ textField: UITextField) { // 選択されているTextFieldを更新 self.selectedTextField = textField } // リターンが押された時 func textFieldShouldReturn(_ textField: UITextField) -> Bool { // キーボードを閉じる textField.resignFirstResponder() return true } // moveSize分Y方向にスクロールさせる func updateScrollViewSize(moveSize: CGFloat, duration: TimeInterval) { UIView.beginAnimations("ResizeForKeyboard", context: nil) UIView.setAnimationDuration(duration) let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: moveSize, right: 0) self.commentofscrollView.contentInset = contentInsets self.commentofscrollView.scrollIndicatorInsets = contentInsets self.commentofscrollView.contentOffset = CGPoint(x: 0, y: moveSize) UIView.commitAnimations() } func restoreScrollViewSize() { // キーボードが閉じられた時に、スクロールした分を戻す self.commentofscrollView.contentInset = UIEdgeInsets.zero self.commentofscrollView.scrollIndicatorInsets = UIEdgeInsets.zero } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/07/04 14:02

ぱっと見疑問なのですが。なんでstoryboardに存在するオブジェクトをviewDidLoad内でaddSubviewしてるの?
guest

回答1

0

ベストアンサー

結局のところ、分かりません。

投稿2020/07/04 13:52

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問