当 UITextField 在执行自定义 segue 后成为第一响应者时,键盘出现两次

Keyboard appears twice when UITextField becomes first responder after custom segue performed

所以问题是:当我在源视图上按下按钮时,自定义转场执行。

这是代码(非常标准):

class FirstCustomSugue: UIStoryboardSegue {
override func perform() {

    let initalView = self.source.view as UIView?
    let destinationView = self.destination.view as UIView?

    let screenHeight = UIScreen.main.bounds.size.height
    let screenWidth = UIScreen.main.bounds.size.width

    initalView?.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
    destinationView?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight)

    let appWindow = UIApplication.shared.keyWindow
    appWindow?.insertSubview(destinationView!, aboveSubview: initalView!)

    UIView.animate(withDuration: 0.4, animations: {
        // Left/Right
        initalView?.frame = (initalView?.frame.offsetBy(dx: -screenWidth, dy: 0))!
        destinationView?.frame = (destinationView?.frame.offsetBy(dx: -screenWidth, dy: 0))!
    }) { (Bool) in
        self.source.present(self.destination, animated: false, completion: nil)
    }
}

}

我在 viewDidiAppear 中调用 myTextField.becomeFirstResponder()(包括对 super 的调用)。

当我打开模拟器的“慢速动画”时,我可以清楚地看到键盘首先出现在动画旁边,动画完成后它会重新出现。

我将示例包含在转场中,因为当我切换到标准转场时就没有这样的问题。如果您需要任何其他代码示例,我将更新 post.

使用自定义 segues 因为我没有使用 navigationController 我在视图之间转换,默认模式 segues 不是一个选项。

我的猜测是 viewDidAppear 被调用了两次,因为即使我在 viewWillAppearviewDidLoad 中设置了 myTextField.isUserInteractionEnabled = falsemyTextField.isEnabled = false (之前或之后两种方法中的 super),然后在调用 super 后立即在 viewDidAppear 中将它们设置为 true 结果是相同的。

提前TNX!

将 segue 的功能更改为以下内容:

    let sourceViewController = self.source
    let destinationViewController = self.destination

    let transition = CATransition()

    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromRight

    let appWindow = UIApplication.shared.keyWindow
    appWindow?.layer.add(transition, forKey: kCATransition)

    sourceViewController.present(destinationViewController, animated: false, completion: nil)ere