Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

前端 未结 10 1082
被撕碎了的回忆
被撕碎了的回忆 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:43

    In swift4 we can create an extension of UIViewController.

    extension UIViewController {
        //MARK: Keyboard user interactions
        func handleContainerViewFrameOnKeyboardShowHide(originalContainerFrame: CGRect) {
            NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillShow, object: nil, queue: OperationQueue.main) {(notification) in
                var info = (notification as NSNotification).userInfo!
                let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
                var tableViewFrame = originalContainerFrame
                tableViewFrame.size.height = originalContainerFrame.size.height - keyboardFrame.size.height
                self.view.frame = tableViewFrame
            }
            NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: OperationQueue.main) { (notification) in
                self.view.frame = originalContainerFrame
            }
        }
    }
    

提交回复
热议问题