键盘出现时如何防止弹出窗口滚动

时间:2018-02-15 13:32:09

标签: ios swift keyboard popover eureka-forms

Scroll Problem

我有一个表单(Eureka表单,在UITableView中),我在iPad中显示为Popover。但是当键盘出现时,Tableview会向下滚动并隐藏我正在编辑的字段。

当弹出框改变它们的大小时,有没有办法防止这种滚动?或者再次关注我正在编辑的字段?

这是主视图控制器中的代码

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

这是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 个答案:

答案 0 :(得分:0)

键盘出现后,您应该听 UITextFieldDelegate 。该委托函数将被称为

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

答案 1 :(得分:0)

最后,我解决了覆盖keyboardWillShow的问题。现在,如果我不调用“超级”,UITableView就不会滚动。

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

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