Jay_Jordz Jay_Jordz - 9 days ago 5
iOS Question

How to set size of UIScrollView?

I'm following some online examples I found on how to setup a UI Scroll View and have run into a problem with setting the size of the Scroll View. I'm attempting to set the size to height = height of screen and width = 3x width of screen. Here is the code I'm attempting, but CGSizeMake is no longer supported in Swift3. Any suggestions on how to accomplish this without CGSizeMake?

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


Thanks!

Nik Nik
Answer

CGSizeMake is deprecated in Swift 3. Use regularCGSize instead. Your code should look like this:

self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)

The main difference between them is that you have to type in the parameter names (width, and height) for CGSize, while you do not need to for CGSizeMake.

You can take a look at this question for additional information: What's the difference between using CGSizeMake and CGSize? Is one better than the other?

Simply put, CGSize, is used more commonly in Swift because it's considered more "Swifty". CGSizeMake is a leftover from Objective-C