自定义UIStoryboardSegue - UIViewController supportedInterfaceOrientations永远不会调用

时间:2016-03-03 18:26:15

标签: ios swift storyboard

我在创建自定义segue时遇到问题:我重写了" perform()"方法如下

override func perform() {
    if !isBack {
        self.destinationViewController.view.frame = CGRectMake(self.sourceViewController.view.frame.width, 0, self.sourceViewController.view.frame.width, self.sourceViewController.view.frame.height)
        self.sourceViewController.view.addSubview(self.destinationViewController.view)
        self.sourceViewController.addChildViewController(self.destinationViewController)
        print("Source: \(self.sourceViewController) ---> Destination: \(self.destinationViewController)")
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.destinationViewController.view.frame = CGRectMake(0, 0, self.destinationViewController.view.frame.width, self.destinationViewController.view.frame.height)
            }, completion: { (bolean) -> Void in
                print("Animation Completed")
        })

    }else{
        print("Source: \(self.sourceViewController) ---> Destination: \(self.destinationViewController)")
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.sourceViewController.view.frame = CGRectMake(self.sourceViewController.view.frame.width, 0, self.sourceViewController.view.frame.width, self.sourceViewController.view.frame.height)
            }, completion: { (bolean) -> Void in
                print("Animation Completed")

                self.sourceViewController.view.removeFromSuperview()
        })
    }

一切运行正常,但此时," destinationViewController"中的旋转方法没有被召唤。所以,如果我锁定" sourceViewController"离开" destinationViewController"到肖像模式解锁后者不会旋转。

假设问题出在此行: self.sourceViewController.view.addSubview(self.destinationViewController.view) 我该如何更改代码以修复旋转?

1 个答案:

答案 0 :(得分:0)

好的家伙我找到了这样的解决方案。

override func perform() {
    if !isBack {
        self.destinationViewController.view.frame = CGRectMake(self.sourceViewController.view.frame.width, 0, self.sourceViewController.view.frame.width, self.sourceViewController.view.frame.height)

        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(self.destinationViewController.view, aboveSubview: self.sourceViewController.view)

        print("Source: \(self.sourceViewController) ---> Destination: \(self.destinationViewController)")
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.destinationViewController.view.frame = CGRectMake(0, 0, self.destinationViewController.view.frame.width, self.destinationViewController.view.frame.height)
            }, completion: { (bolean) -> Void in
                print("Animazione Terminata")
                self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
        })

    }else{
        print("Indietro Source: \(self.sourceViewController) ---> Destination: \(self.destinationViewController)")

        let snapshot = self.sourceViewController.view.snapshotViewAfterScreenUpdates(false)
        snapshot.frame = CGRectMake(0, 0, snapshot.frame.size.width, snapshot.frame.size.height)
        self.destinationViewController.view.addSubview(snapshot)
        self.sourceViewController.dismissViewControllerAnimated(false, completion: nil)
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                snapshot.frame = CGRectMake(self.destinationViewController.view.frame.width, 0, self.destinationViewController.view.frame.width, self.destinationViewController.view.frame.height)
                }, completion: { (bolean) -> Void in
                    print("Animazione Terminata")
                    snapshot.removeFromSuperview()
            })
        })
    }
}

我已经明白UIStoryboar最终会像演示一样做同样的操作。所以在你必须创建动画之前,然后出现。当你需要在获取屏幕快照之前解散而没有动画时解除源VC(在解除之前在目标VC中添加快照)并最终为快照设置动画。

tnx并购买。

抱歉我的英语不好但是说话就是芯片我会显示代码。

编辑:我的代码中有一个小小的不精确。为了防止动画问题,只需在当前内部设置o dismiss complanion你的动画。