直接从 textField 移动到 textField 时 UITableView 不会移动

UITableView doesn't shift when moving from textField to textField directly

我目前正在与基于 Swift 的 iPhone 应用程序中的一些 textField 键盘问题作斗争。

这张图片显示了主要问题(前三张图片显示了问题,后三张是它应该做的):

编辑tableViewCell 中的textField 时,我想将整个tableview 和导航栏向上移动。我可以使用下面的代码(摘自 viewController):

var tableViewShiftedUp = false

...

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

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! QuantityTableViewCell

        let componentLocationQuantity = tableViewData[indexPath.row]

        if let locationString = componentLocationQuantity.location.valueForKey("location_name") as? String {
            cell.setCellContents(locationString, quantity: componentLocationQuantity.quantity.floatValue)
            cell.quantityLabel.delegate = self
        }

        //get stock level related to current build rate and lead time (good - green, warning - orange, urgent - red)
        let status = model.determineStockLevelStatus(component)
        cell.setQuantityLabelBackgroundStatusColor(status)

        if editingMode == true {
            cell.makeQuantityEditable()
        }

        //add control event to detect text change
        cell.quantityLabel.addTarget(self, action: #selector(quantityCellTextChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)
        cell.quantityLabel.addTarget(self, action: #selector(quantityCellSelected(_:)), forControlEvents: UIControlEvents.EditingDidBegin)


        return cell
    }

...

//MARK: - UITextfield delegate
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)

        if tableViewShiftedUp == true {
            moveTable()
        }

    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    func quantityCellSelected(textField: UITextField){
        if tableViewShiftedUp == false {
            moveTable()
        }
        //check table was moved
        print(tableView.frame.origin.y)
    }

    func hideKeyboard(){
        self.view.endEditing(true)
        if tableViewShiftedUp == true {
            moveTable()
        }
    }

    func moveTable(){
        if tableViewShiftedUp == true {
            animateTableViewMoving(false, moveValue: 250)
            animateNavigationBarMoving(false, moveValue: 250)
            tableViewShiftedUp = false
        } else {
            animateTableViewMoving(true, moveValue: 250)
            animateNavigationBarMoving(true, moveValue: 250)
            tableViewShiftedUp = true
        }
    }

    // moving the tableView
    func animateTableViewMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:NSTimeInterval = 0.0
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.tableView.frame = CGRectOffset(self.tableView.frame, 0,  movement)
        UIView.commitAnimations()
    }
    // moving the navigationBar
    func animateNavigationBarMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:NSTimeInterval = 0.0
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.midNavigationBar.frame = CGRectOffset(self.midNavigationBar.frame, 0,  movement)
        UIView.commitAnimations()
    }

直接移动到 tableView 中的 textField 时效果很好。但是,当我已经在 tableView 之外编辑 textField 时,不会发生向上移动,因此移动切换不同步。

我打印了 tableView 框架的来源:

//check table was moved
print(tableView.frame.origin.y)

所以我可以看到 tableView 的设置,并且在第一次从 tableView 外的 textField 移动到 tableView 内的 textField 时,这个 属性 是我期望的 134,但它仍然是在下次调用时打印的屏幕上的 384。

在 tableView 的单元格内移动时不会出现此问题。

感觉我遇到了某种竞争条件问题,或者我错过了一些被调用的委托方法,当从一个单元格转换到下一个单元格时,它会抛出所有内容。

帮助会很大。

当你使用自动布局时你不能像你那样直接调整框架的大小,你必须使用约束(你可以为约束创建IBOutlets在 IB 中创建或添加新的,然后从动画块中将 layoutIfNeeded 发送到您的视图)。

您可以按照 Apple 官方说明进行操作:

查看此图书馆:https://github.com/michaeltyson/TPKeyboardAvoiding。 旨在让键盘远离文本字段。