Search code examples
swiftswift3cgsize

Specific issue for CGSizeMake with Swift 3 Xcode 8


I'm having an issue with the youtube video by Jared Davidson. https://www.youtube.com/watch?v=1_daE3IL_1s At exactly 7 minutes, he writes the code line:

self.ScrollView1.contentSize = CGSizeMake(self.view.frame.width * 2, self.view.frame.size.height)

My error is showing that CGSizeMake is unavailable in Swift, but in the video it works perfectly. Here is my version of the code:

import UIKit
class VC1: UIViewController {
    @IBOutlet weak var ScrollView1: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        var V1 : View1 = View1(nibName: "View1", bundle: nil)
        var V2 : View2 = View2(nibName: "View2", bundle: nil)
        var V2Frame : CGRect = V2.view.frame

        self.addChildViewController(V1)
        self.ScrollView1.addSubview(V1.view)
        V1.didMove(toParentViewController: self)

        self.addChildViewController(V2)
        self.ScrollView1.addSubview(V2.view)
        V2.didMove(toParentViewController: self)

        V2Frame.origin.x = self.view.frame.width
        V2.view.frame = V2Frame

        self.ScrollView1.contentSize = CGSizeMake(self.view.frame.width * 2, self.view.frame.size.height)
    }
}

Solution

  • Proper usage of CGSize in Swift 3:

    let size = CGSize(width: x, height: y)
    

    In your case:

    self.ScrollView1.contentSize = CGSize(width: self.view.frame.width * 2, height: self.view.frame.size.height)
    

    CGSizeMake is deprecated.

    (P.S. it's good practice to use camel case to name variables (e.g. "scrollView1" instead of "ScrollView1"))