切换视图控制器提供额外的覆盖

时间:2016-12-19 11:06:51

标签: ios swift

我不确定我的app委托中发生了什么我完成此代码显示我的服务视图

  func showServiceStartView()
    {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SERVICE_START_VC_ID") 
        self.window!.rootViewController = secondViewController
        self.window!.makeKeyAndVisible()
    }

但它给了我奇怪的行为。当视图切换时,它显示额外的覆盖几秒钟然后消失。但我不想要额外的叠加。

http://giphy.com/gifs/l0MYFCQ3LwV8r90m4

我正在努力推动这一个Programmatically set the initial view controller using Storyboards

2 个答案:

答案 0 :(得分:1)

额外的叠加层来自上一个屏幕。如果您不想要那种叠加层,则必须使用自定义过渡。以下是交换根视图控制器的代码

对于Swift 3.0:

    func changeRootViewController(with identifier:String!) {
    let storyboard = self.window?.rootViewController?.storyboard
    let desiredViewController = storyboard?.instantiateViewController(withIdentifier: identifier);

    let snapshot:UIView = (self.window?.snapshotView(afterScreenUpdates: true))!
    desiredViewController?.view.addSubview(snapshot);

    self.window?.rootViewController = desiredViewController;

    UIView.animate(withDuration: 0.3, animations: {() in
      snapshot.layer.opacity = 0;
      snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
      }, completion: {
        (value: Bool) in
        snapshot.removeFromSuperview();
    });
  }

对于Swift 2.2:

 func changeRootViewControllerWithIdentifier(identifier:String!) {
let storyboard = self.window?.rootViewController?.storyboard
let desiredViewController = storyboard?.instantiateViewControllerWithIdentifier(identifier);

let snapshot:UIView = (self.window?.snapshotViewAfterScreenUpdates(true))!
desiredViewController?.view.addSubview(snapshot);

self.window?.rootViewController = desiredViewController;

UIView.animateWithDuration(0.3, animations: {() in
  snapshot.layer.opacity = 0;
  snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
  }, completion: {
    (value: Bool) in
    snapshot.removeFromSuperview();
});

}

答案 1 :(得分:0)

不要重新初始化窗口,只需更新rootViewController对象,它应该没问题。

func showServiceStartView()
    {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SERVICE_START_VC_ID") 
        self.window!.rootViewController = secondViewController
    }

您可以在更改rootViewController时添加转换动画。

UIView.transition(with: window!, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromBottom, animations: {
                    self.window?.rootViewController = secondViewController
                 }, completion: nil)