cristan lika
cristan lika

Reputation: 425

Switching view controller gives extra overlay

I am not sure what is going on here in my app delegate I done this code show my service view

  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()
    }

But it gives me weird behaviour. When view switch then it shows extra overlay for few second then go away. But I don't want extra overlay.

http://giphy.com/gifs/l0MYFCQ3LwV8r90m4

i am working flowing this one Programmatically set the initial view controller using Storyboards

Upvotes: 0

Views: 256

Answers (2)

Sivajee Battina
Sivajee Battina

Reputation: 4174

That extra overlay is from the previous screen. If you don't want that kind of overlay, you have to use custom transition for that. Here is the code for swapping of root view controllers

For 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();
    });
  }

For 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();
});

}

Upvotes: 1

Zaid Pathan
Zaid Pathan

Reputation: 16820

Don't re-initialize window, just update rootViewController object, and it should be fine.

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

You can add transition animation while changing rootViewController.

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

Upvotes: 0

Related Questions