0

I wrote an iOS-App that uses multiple Views. Since I did not want to use the storyboard, I created a set of Views (xib files) and corresponding ViewControllers (swift files that extend UIViewController).

The views are created with all "Simulation Metrics" set to inferred, "Use Auto Layout" is on and "Use Size Classes" is also on. For every element, constraints are set, no errors.

In func application(application: UIApplication, didFinishLaunchingWithOptions ...) in AppDelegate.swift, I initiate my ViewControllers, e.g. by connectVC = ConnectViewController(nibName: "ConnectView", bundle: nil)

On user actions (e.g. button click) and network actions (e.g. game started), I switch views programmatically with an animation by changing the window.rootViewController in AppDelegate.swift, exactly like here but in Swift.

So far, so good. But whenever I show a view for the first time, the view appears with the scaling/size just like in my Xcode interface builder, then resizes about half a second to a second later to the correct size of device's screen. This happens in the iOS Simulator as well as on my real iPhone.

The views use quite a few images in UIImageViews, but reducing the resolution or amount of pictures does not change that behaviour.

I would like to speed up that resize, ideally the users should not see any resizing of elements in my view. How can I do that?

Community
  • 1
  • 1
CrypticDots
  • 314
  • 1
  • 8
  • I think for that storyboard is best option as well as don't change root view controller. And where you wrote code about side ? – Pravin Tate Jul 06 '15 at 15:52
  • Might be a threading thing. You could try using something like `dispatch_async(dispatch_get_main_queue(), {YOUR_CODE})` – boidkan Jul 06 '15 at 16:07
  • Alright I found out that if I do not use `UIView.transitionFromView()`, everything looks fine. Also, when using `UIView.transitionFromView()` with Option `.ShowHideTransitionViews`, it resizes correctly but it fades to black which does not look good. – CrypticDots Jul 06 '15 at 16:39

2 Answers2

0

This happened to me. First go into your GameViewController that starts up with the app. Then find this code:

if let scene = GameScene(fileNamed:"GameScene") {
    // Configure the view.
    let skView = self.view as! SKView!
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = true
    skView.showsFPS = false
    skView.showsNodeCount = false
    /* Set the scale mode to scale to fit the window */
    scene.scaleMode = .ResizeFill
    skView.presentScene(scene)
}

Change all of that code to this:

if let skView = self.view as! SKView! {
    let scene = GameScene(size: self.view.bounds.size)
    skView.ignoresSiblingOrder = true
    skView.showsFPS = false
    skView.showsNodeCount = false
    scene.scaleMode = .ResizeFill
    scene.size = skView.bounds.size
    skView.presentScene(scene)
}

That should work, if you change GameScene to what ever your first scene is.

grahamcracker1234
  • 591
  • 1
  • 7
  • 27
0

The problem was that I was using the ViewController chain incorrectly. Simply changing the window.rootViewController and doing a UIView.transitionFromView is not the indented way to deal with view switching.

So I created a SuperViewController that contains instances of all other ViewControllers and use the two functions below to switch between views. Now works like a charm.

The Creating Custom Container View Controllers article and also View Controller Programming Guide for iOS from Apple helped me quite a lot.

func setViewController(vc: UIViewController) {
    self.addChildViewController(vc)
    vc.view.frame = self.view.bounds
    self.view.addSubview(vc.view)
    vc.didMoveToParentViewController(self)
    self.currentViewController = vc
}

func switchViewController(from: UIViewController, to: UIViewController) {
    from.willMoveToParentViewController(nil)
    self.addChildViewController(to)
    to.view.frame = self.view.bounds
    self.transitionFromViewController(from, toViewController: to, duration: 0.5, options: .TransitionCrossDissolve, animations: nil) { finished in
        from.removeFromParentViewController()
        to.didMoveToParentViewController(self)
        self.currentViewController = to
    }
}
CrypticDots
  • 314
  • 1
  • 8