关于ios:如何防止出现键盘时弹出窗口滚动

How to prevent popover from scrolling when Keyboard Appears

Scroll Problem

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

当弹出框更改其大小时,是否可以防止这种滚动?还是重新聚焦我正在编辑的字段的方法?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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形式的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)
    }
}


最后,我解决了覆盖keyboardWillShow的问题。现在,如果我不叫" super",则UITableView不会滚动。

1
2
3
4
5
6
7
8
9
override func keyboardWillShow(_ notification: Notification) {
    print("Keyboard Will Show")
   // super.keyboardWillShow(notification)
}

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


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

1
2
3
4
5
6
7
8
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)
}