存在键盘时,选择导致视图向上滑动的原因

时间:2019-04-07 16:20:07

标签: swift user-interface uikit

我有一个tableView,第0行中有一个textField,第3行中有textView,每次当有键盘时,我的tableview都会向上滑动。当tableView向上滑动时,您将在第0行中看不到textField。如何禁用第0行,而仅保留第3行?我尝试使用Protocol&Delegates尝试仅对第3行封装该函数,但这不起作用。

enter image description here     类CreateEditItemController:UIViewController,CreateItemDescriptionCellDelegate {

@IBOutlet weak var tableView: UITableView!

func handleKeyboardShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

        if self.view.frame.origin.y == 0 {
            //self.view.frame.origin.y -= keyboardSize.height
            self.view.frame.origin.y -= 200
        }
    }
}

func handleKeyboardHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    ...
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateItemDescriptionCell", for: indexPath) as! CreateItemDescriptionCell
        cell.delegate = self
        return cell
    default:
        return tableView.cellForRow(at: indexPath)!
     }
   }
 }


protocol CreateItemDescriptionCellDelegate: class {
    func handleKeyboardShow(notification: NSNotification)
    func handleKeyboardHide(notification: NSNotification)
}

class CreateItemDescriptionCell: UITableViewCell, UITextViewDelegate {
//IBOUTLETS
@IBOutlet weak var notesTextView: UITextView!
weak var delegate: CreateItemDescriptionCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    notesTextView.delegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func handleKeyboardShow(notification: NSNotification) {
    delegate?.handleKeyboardShow(notification: notification)
}

@objc func handleKeyboardHide(notification: NSNotification) {
    delegate?.handleKeyboardHide(notification: notification)
 }
}

2 个答案:

答案 0 :(得分:1)

经过一些数学运算后,您尝试做的事情是可能的,但是我建议为此使用第三方容器,而不是在Evert控制器上手动进行此操作。

将此添加到您的pod文件中:

# IQKeyboardManager: Codeless drop-in universal library allows to prevent issues of keyboard sliding up
# https://github.com/hackiftekhar/IQKeyboardManager
pod 'IQKeyboardManagerSwift'   

有关更多详细信息和文档视图: https://github.com/hackiftekhar/IQKeyboardManager

您唯一需要写的行是:

// Enabling IQKeyboardManager
IQKeyboardManager.shared.enable = true

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 

这将解决您所有的问题,而您不必计算框架或任何其他东西。

答案 1 :(得分:1)

您可以选择一些路线,但是为简单起见,您可以使用一些库为您完成所有计算,而您不必担心。

我一直使用的一个是:

async function fetchData(offset = 0, maxLenReturn = 200) { let response = await fetch(asyncCall) let data = await response.json(); let inbox = data.Body.Conversations; let fullLength = data.Body.TotalConversationsInView if (offset < fullLength-maxLenReturn) { offset+= maxLenReturn inbox.push(...await fetchData(offset)) } return inbox; } let fullInbox = fetchData() -https://github.com/michaeltyson/TPKeyboardAvoiding

TPKeyboardAvoiding-https://github.com/hackiftekhar/IQKeyboardManager

还有许多其他人。