0

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

Community
  • 1
  • 1
cristan lika
  • 415
  • 1
  • 7
  • 22
  • how do you push - pop view controller? In code. – Mohammad Zaid Pathan Dec 19 '16 at 11:13
  • @ZaidPathan button against call this method – cristan lika Dec 19 '16 at 11:23
  • Go to your `AppDelegate.Swift` file and you will see it *already* has `var window: UIWindow?` so you are now working with another. You should always have only one window. *Most apps need only one window, which displays the app’s content on the device’s main screen. You can create additional windows and display them on the device’s main screen, but extra windows are more commonly used to display content on an attached external display*. Read more [here](https://developer.apple.com/reference/uikit/uiwindow) – mfaani Dec 19 '16 at 12:32

2 Answers2

1

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

}

Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
0

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)
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130