键盘出现时如何防止弹出窗口滚动
How to prevent popover from scrolling when Keyboard Appears
我有一个表单(尤里卡表单,在 UITableView 中),我在 iPad 中显示为弹出窗口。但是当键盘出现时,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(){
[=11=].title = "Initial Flowrate"
[=11=].placeholder = "Enter initial flowrate"
}
<<< IntRow(){
[=11=].title = "Final Flowrate"
[=11=].placeholder = "Enter final flowrate"
}
+++ Section("Time")
<<< IntRow() {
[=11=].title = "Minutes"
[=11=].placeholder = "min"
}
<<< IntRow() {
[=11=].title = "Seconds"
[=11=].placeholder = "sec"
}
+++ Section("Repeat")
<<< SwitchRow(){
[=11=].title = "Repeat"
}
<<< CountDownRow() {
[=11=].title = "Hello"
}
+++ Section("Section2")
<<< DateRow(){
[=11=].title = "Date Row"
[=11=].value = Date(timeIntervalSinceReferenceDate: 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)
}
最后我解决了这个覆盖 keyboardWillShow 的问题。现在,如果我不调用 "super".
,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)
}
我有一个表单(尤里卡表单,在 UITableView 中),我在 iPad 中显示为弹出窗口。但是当键盘出现时,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(){
[=11=].title = "Initial Flowrate"
[=11=].placeholder = "Enter initial flowrate"
}
<<< IntRow(){
[=11=].title = "Final Flowrate"
[=11=].placeholder = "Enter final flowrate"
}
+++ Section("Time")
<<< IntRow() {
[=11=].title = "Minutes"
[=11=].placeholder = "min"
}
<<< IntRow() {
[=11=].title = "Seconds"
[=11=].placeholder = "sec"
}
+++ Section("Repeat")
<<< SwitchRow(){
[=11=].title = "Repeat"
}
<<< CountDownRow() {
[=11=].title = "Hello"
}
+++ Section("Section2")
<<< DateRow(){
[=11=].title = "Date Row"
[=11=].value = Date(timeIntervalSinceReferenceDate: 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)
}
最后我解决了这个覆盖 keyboardWillShow 的问题。现在,如果我不调用 "super".
,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)
}