Search code examples
iosswiftkeyboardpopovereureka-forms

How to prevent popover from scrolling when Keyboard Appears


Scroll Problem

I have a form (Eureka form, in a UITableView) that I show in a iPad as a Popover. But when the keyboard appears, the Tableview scrolls down and hides the field that I am editing.

Is there a way to prevent that kind of scroll when the popover change their size? Or a way to focus again the field that I am editing?

This is the code in the Main View Controller

func presentFormPopover(form: FormViewController) {

    let nav = UINavigationController(rootViewController: form)
    nav.modalPresentationStyle = .popover
    let popover = nav.popoverPresentationController!

    popover.sourceRect = CGRect(x: view.center.x, y: view.center.y, width: 0, height: 0)
    popover.sourceView = self.view
    popover.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
    popover.delegate = self
    popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
    self.present(nav, animated: true, completion: nil)
}

/* Reposition the popover if the screen rotates */

public func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {
    let x = popoverPresentationController.presentingViewController.view.center
    let newRect = CGRect(x: x.x, y: x.y, width: 0, height: 0)
    rect.initialize(to: newRect)
}

This is the code of the form of the Popover:

import Eureka

class AddStepTaskFormViewController: FormViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    self.title = "New Step Task"

    form +++ Section("Flowrate")
        <<< IntRow(){
            $0.title = "Initial Flowrate"
            $0.placeholder = "Enter initial flowrate"
        }
        <<< IntRow(){
            $0.title = "Final Flowrate"
            $0.placeholder = "Enter final flowrate"
        }
        +++ Section("Time")

        <<< IntRow() {
            $0.title = "Minutes"
            $0.placeholder = "min"
        }
        <<< IntRow() {
            $0.title = "Seconds"
            $0.placeholder = "sec"
        }
        +++ Section("Repeat")

        <<< SwitchRow(){
            $0.title = "Repeat"
        }

        <<< CountDownRow() {
            $0.title = "Hello"
        }
        +++ Section("Section2")
        <<< DateRow(){
            $0.title = "Date Row"
            $0.value = Date(timeIntervalSinceReferenceDate: 0)
    }
}

Solution

  • Finally I solved this issue overriding the keyboardWillShow. Now the UITableView not does scroll if I do not call "super".

    override func keyboardWillShow(_ notification: Notification) {
        print("Keyboard Will Show")
       // super.keyboardWillShow(notification)
    }
    
    override func keyboardWillHide(_ notification: Notification) {
        print("Keyboard Will Hide")
        //super.keyboardWillHide(notification)
    }