Théo Fleismaher Théo Fleismaher - 4 months ago 42
iOS Question

Swift swipe navigation adjusting view controller size

I'm trying to make a swipe navigation with view controllers instead of xib files using this tutorial : https://www.youtube.com/watch?v=3jAlg5BnYUU

I've managed to replace the xib files with view controllers but when I run the app the width of the view controllers is smaller than normal :

Screenshot

I don't understand as it is set identically as with the xib files. Anyone knows how to solve this?

Here is my code :

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

self.addChildViewController(vc0!)
self.scrollView.addSubview(vc0!.view)
vc0!.didMoveToParentViewController(self)


let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

var frame1 = vc1!.view.frame
frame1.origin.x = self.view.frame.size.width
vc1!.view.frame = frame1

self.addChildViewController(vc1!)
self.scrollView.addSubview(vc1!.view)
vc1!.didMoveToParentViewController(self)


let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

var frame2 = vc2!.view.frame
frame2.origin.x = self.view.frame.size.width * 2
vc2!.view.frame = frame2

self.addChildViewController(vc2!)
self.scrollView.addSubview(vc2!.view)
vc2!.didMoveToParentViewController(self)


let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

var frame3 = vc3!.view.frame
frame3.origin.x = self.view.frame.size.width * 3
vc3!.view.frame = frame3

self.addChildViewController(vc3!)
self.scrollView.addSubview(vc3!.view)
vc3!.didMoveToParentViewController(self)


self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

Answer

Notice that using viewDidLoad for set frame manually is a bit risky and should be avoided since the bounds are not set. Instead use the viewDidLayoutSubviews to do that but take a look because the method is called all the time when the UI elements change including constraints.

    var scrollViewAdded = false

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    if !scrollViewAdded {

        self.loadSrollView()

        self.scrollViewAdded = true
    }
}


func loadSrollView() {

    self.scrollView.pagingEnabled = true

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

    self.addChildViewController(vc0!)
    self.scrollView.addSubview(vc0!.view)
    vc0!.didMoveToParentViewController(self)

    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.scrollView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)

    let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

    var frame2 = vc2!.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2!.view.frame = frame2

    self.addChildViewController(vc2!)
    self.scrollView.addSubview(vc2!.view)
    vc2!.didMoveToParentViewController(self)

    let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

    var frame3 = vc3!.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc3!.view.frame = frame3

    self.addChildViewController(vc3!)
    self.scrollView.addSubview(vc3!.view)
    vc3!.didMoveToParentViewController(self)

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}
Comments