How to prevent popover from scrolling when Keyboar

2019-08-23 09:36发布

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)
    }
}

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-23 09:47

You should listen for UITextFieldDelegate once the keyboard presented. This delegate function will be called

func textFieldDidBeginEditing(_ textField: UITextField) {
    // Find the cell indexPath and ask the table view to scroll to that cell.
    // indexpath for the cell containing your textField
    let indexPath = IndexPath(row: 0, section: 0)
    // you can change the at .middle, .top, .bottom
    // also animated set to false so user don't see scrolling
    tableView.scrollToRow(at: indexPath, at: .none, animated: false)
}
查看更多
小情绪 Triste *
3楼-- · 2019-08-23 09:52

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)
}
查看更多
登录 后发表回答