如何在另一个UIViewController之上呈现UIViewController,但不占用全屏

时间:2017-11-29 11:46:44

标签: ios swift uiviewcontroller

Required final image我正在编写一个自定义UIStoryboardSegue,我打算在Source ViewController上使用动画呈现Destination ViewController。这是动画的代码

    override func perform() {
        let topVCView = self.destination.view
        let bottomVCView = self.source.view

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

        topVCView!.frame  = CGRect(x: 0.0 - screenWidth, y: 0, width: 150.0, height: screenHeight)

        let window = UIApplication.shared.keyWindow
window?.insertSubview(topVCView!, aboveSubview: bottomVCView!)
        window?.makeKeyAndVisible()

        UIView.animate(withDuration: 0.2, animations: {
            topVCView!.frame = topVCView!.frame.offsetBy(dx: screenWidth, dy: 0.0)
        }, completion: ({(finished) -> Void in


        }))

    }

动画效果很好。但我无法在目的地VC中对UIButton个动作进行回调。我怎样才能让它发挥作用?

编辑:红色VC是我的源VC,绿色VC是我的目标VC

1 个答案:

答案 0 :(得分:0)

override func perform() {
            let contentVC = self.sourceViewController;
            let sideBarVC = self.destinationViewController;

            var sideFrame = sideBarVC.view.frame

            sideBarVC.view.frame = CGRect(x: -(contentVC.view.frame.size.width), y: 0, width: contentVC.view.frame.size.width/2, height: contentVC.view.frame.size.height)

            sideFrame = sideFrame.offsetBy(dx: -1 * contentVC.view.frame.size.width, dy: 0)
            contentVC.view.superview?.addSubview(sideBarVC.view)

            UIView.animate(withDuration: 0.3) {
                sideBarVC.view.frame = sideFrame
                contentVC.view.alpha = 0.5
            }
        }