Swift无法从上到下呈现视图控制器

时间:2017-01-03 10:06:05

标签: ios swift swift3 catransition

在我的应用程序中,我必须从上到下呈现屏幕,我尝试了下面的代码,它给出了相同的正常呈现风格。

        let screen = self.storyboard?.instantiateViewController(withIdentifier: "Screen1p5") as? Screen1p5
        let transition = CATransition()
        transition.duration = 0.5
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromTop
        view.window!.layer.add(transition, forKey: kCATransition)
        self.present(screen!, animated: true, completion: nil)

2 个答案:

答案 0 :(得分:11)

为此,您需要将subtype CATransition设置为kCATransitionFromBottom,将面糊动画设置animated设置为false present(_:animated:completion:)。< / p>

let screen = self.storyboard?.instantiateViewController(withIdentifier: "Screen1p5") as? Screen1p5
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
view.window!.layer.add(transition, forKey: kCATransition)
self.present(screen!, animated: false, completion: nil)

subtype的{​​{1}}设置为CATransition

kCATransitionFromTop

答案 1 :(得分:3)

刚刚将其transition.subtype更改为kCATransitionFromBottom

  

transition.subtype = kCATransitionFromBottom

关闭控制器。

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromTop
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: true, completion: nil)

请查看以下GIF表示。

GIF

  

如果您使用的是.XIB,请找到以下代码。

目前是控制器。

let newController = NewViewController(nibName: "NewView", bundle: nil)
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
view.window!.layer.add(transition, forKey: kCATransition)
self.present(newController, animated: true, completion: nil)

关闭控制器。它与上面的代码相同。

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromTop
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: true, completion: nil)