gpt4 book ai didi

iOS 10 - UITableViewController 在加载时滚动到底部

转载 作者:可可西里 更新时间:2023-10-31 23:14:30 24 4
gpt4 key购买 nike

我有一个 UITableViewController 的子类,它显示多行(准确地说是 8 行),其中顶行是一个包含 UITextField 的自定义单元格。加载 View 后,我通过在该 textfield 上调用 becomeFirstResponder 立即弹出键盘。这一切都很好,直到我从 iOS 9 升级到 iOS 10。现在 View 跳到 View 的底部,键盘覆盖了我的最后两行,我的 TextField 不在可见区域。当我注释掉 becomeFirstResponder 调用时,跳跃消失了,但我当然失去了键盘弹出窗口。这就是我的代码的样子...

class UserProfileTableViewController: UITableViewController {

private var userInfoCell: UserInfoTableViewCell?
...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if indexPath.section == 0 {
userInfoCell = tableView.dequeueReusableCellWithIdentifier("userInfoCell", forIndexPath: indexPath) as? UserInfoTableViewCell

if !editMode {
userInfoCell?.nameField.becomeFirstResponder()
}

return userInfoCell!
}
else if indexPath.section == 1 {
....

然后我的自定义表格 View 单元格看起来像这样......

class UserInfoTableViewCell: UITableViewCell, UITextFieldDelegate,, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var photoImageView: UIImageView!
...

override func awakeFromNib() {
super.awakeFromNib()
// listen for when user touches a textfield
nameField.delegate = self
...
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

...

我尝试过的事情:

  1. viewWillAppear() 中调用 userInfoCell?.nameField.becomeFirstResponder()
  2. viewDidAppear() 中调用 userInfoCell?.nameField.becomeFirstResponder()
  3. UserInfoTableViewCell.awakeFromNib() 中调用 nameField.becomeFirstResponder()
  4. 在调用 userInfoCell?.nameField.becomeFirstResponder() 之后调用 tableView.scrollToRowAtIndexPath()
  5. 在我的 viewWillAppear() 中删除 super.viewWillAppear()

这些都不适合我。

最佳答案

延迟 becomeFirstResponder() 为我解决了这个问题

dispatch_async(dispatch_get_main_queue(), {
userInfoCell?.nameField.becomeFirstResponder()
})

关于iOS 10 - UITableViewController 在加载时滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com