简体   繁体   中英

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!

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

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

The main only 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM