简体   繁体   中英

How to Create a CGSize in Swift?

How can I create a CGSize in Swift? This is what I have tried so far (but doesn't work):

var s:CGSize = {10,20}
var s:CGSize = CGMakeSize(10,20)

Your first attempt won't work because C structs don't exist in Swift. You need:

let size = CGSize(width: 20, height: 30)

Or (before Swift 3 only, and even then, not preferred):

let size = CGSizeMake(20,30)

(Not MakeSize).

As of Swift 3 you can no longer use CGSizeMake

The solution for Swift 3 is var size = CGSize(width: 20, height: 30)

斯威夫特4

let size = CGSize(width: 10, height: 10)

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