【问题标题】:Switching view controller gives extra overlay切换视图控制器提供额外的覆盖
【发布时间】:2016-12-19 11:06:50
【问题描述】:

我不确定我的应用委托中发生了什么我完成了这段代码显示我的服务视图

  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

【问题讨论】:

  • 你如何推 - 弹出视图控制器?在代码中。
  • @ZaidPathan 按钮反对调用此方法
  • 转到您的AppDelegate.Swift 文件,您会看到它已经var window: UIWindow?,所以您现在正在使用另一个文件。您应该始终只有一个窗口。 大多数应用程序只需要一个窗口,在设备的主屏幕上显示应用程序的内容。您可以创建额外的窗口并将它们显示在设备的主屏幕上,但额外的窗口更常用于在连接的外部显示器上显示内容。阅读更多here

标签: ios swift


【解决方案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();
});

}

【讨论】:

  • 你用的是哪个版本的swift?
【解决方案2】:

不用重新初始化窗口,只更新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)

【讨论】:

  • 致命错误:在展开可选值时意外发现 nil
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
相关资源
最近更新 更多