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

时间:2018-04-16 08:29:15

标签: ios swift uitextfield segue becomefirstresponder

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

以下是代码(相当标准):

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)
    }
}

}

我在myTextField.becomeFirstResponder()中呼叫viewDidiAppear(包括对超级电话的调用)。

当我设置模拟器的“慢动画”时,我可以清楚地看到键盘首先出现在动画旁边,动画完成后再次出现。

我将示例包括在segue中,因为当我切换到标准segues时,没有这样的问题。如果您需要任何其他代码示例,我将更新帖子。

使用自定义segues,因为我没有使用navigationController进行视图转换,默认模态segues不是一个选项。

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

TNX提前!

1 个答案:

答案 0 :(得分:0)

将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