Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

前端 未结 10 1076
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 14:17

I have tried most of the examples here on StackOverflow. I also used Apple\'s. The problem I seem to have with them is that they don\'t account for the UITextField being in

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 14:38

    Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController swift 3 and Xcode 8.1

    1) set Delegate = UITextFieldDelegate

      override func viewWillAppear(_ animated: Bool) {
    
            NotificationCenter.default.addObserver(self, selector: #selector(DayViewController.keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
            NotificationCenter.default.addObserver(self, selector: #selector(DayViewController.keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
    
       }
          func textFieldDidBeginEditing(_ textField: UITextField) {
              self.activeField = textField
       }
          func textFieldDidEndEditing(_ textField: UITextField) {
            self.activeField = nil
       }
    
    
    //MARK: - Keyboard Show and Hide Methods
        func keyboardWillShow(notification: NSNotification)
        {
            let info = notification.userInfo! as! [String: AnyObject],
            kbSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size,
            contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height, right: 0)
    
            self.FourthTblMainTableView.contentInset = contentInsets
            self.FourthTblMainTableView.scrollIndicatorInsets = contentInsets
    
            var aRect = self.FourthTblMainTableView.frame
            aRect.size.height -= kbSize.height
        }
        func keyboardWillHide(notification: NSNotification)
        {
             let contentInsets = UIEdgeInsets.zero
            self.FourthTblMainTableView.contentInset = contentInsets
            self.FourthTblMainTableView.scrollIndicatorInsets = contentInsets
        }
    

提交回复
热议问题