UITableViewController not scrolling view when keyboard appears

前端 未结 4 1626
南旧
南旧 2020-12-14 10:17

I have a UITableViewController with around 20 static cells, some of these cells have UITextFields within them and some are just for selecting with

相关标签:
4条回答
  • 2020-12-14 10:56

    If the auto scroll of UITableViewController doesn't work with the UITextFields in cells or scroll weirdly, do these steps. Swift 5 iOS 13.2 tested 100%

    First implement viewWillAppear but don't call super.viewWillAppear (this will stop auto scroll)

    override func viewWillAppear(_ animated: Bool) {
    
    }
    

    Then let's do the scroll manually.

    override func viewWillAppear(_ animated: Bool) {
    
      //Notification center observers
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)),
                                                 name: UIResponder.keyboardDidShowNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)),
                                                name: UIResponder.keyboardWillHideNotification, object: nil)
    
    }
    
    //keybord show action
    @objc func keyboardWillShow(notification: Notification) {
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: notification.getKeyBoardHeight, right: 0)
    }
    //keyboard hide action
    @objc func keyboardWillHide(notification: Notification) {
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    extension Notification {
    
        var getKeyBoardHeight: CGFloat {
    
            let userInfo: NSDictionary = self.userInfo! as NSDictionary
            let keyboardFrame: NSValue = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
            let keyboardRectangle = keyboardFrame.cgRectValue
            return keyboardRectangle.height
        }
    
    }
    
    override func viewWillDisappear(_ animated: Bool) {
       super.viewWillDisappear(animated)
       NotificationCenter.default.removeObserver(self)
    }
    
    0 讨论(0)
  • 2020-12-14 11:00

    I ran into this issue myself. I just converted my view controller from a UIViewController to a UITableViewController in addition to adding the [super viewWillAppear:animated]; call, you will need to remove these lines:

    [self.tableView setDataSource:self]; [self.tableView setDelegate:self];

    As they are no longer needed and setDelegate interferes with the keyboard scrolling behavior.

    0 讨论(0)
  • 2020-12-14 11:02

    I found non of these answers to be correct. After a while, I notice that if you push a controller it won't work ... but if you present it modally.. the table will automatically scroll to the used textfield.

    Hope that saves time and stress to anyone.

    0 讨论(0)
  • 2020-12-14 11:10

    Make sure that if you are overriding viewWillAppear that you call

    [super viewWillAppear:animated];
    

    If you don't, the Scroll View will not scroll up properly.

    Swift

    super.viewWillAppear(animated)
    
    0 讨论(0)
提交回复
热议问题