Create CGRect with short way - swift

xarly picture xarly · Aug 28, 2014 · Viewed 20.6k times · Source

When I created CGRect in Objective-c and I didn't want to set each value, I created in this way:

 (CGRect){CGPointMake(0, 0), [UIScreen mainScreen].bounds.size}

Do you know a similar way in swift?

Answer

Mick MacCallum picture Mick MacCallum · Aug 28, 2014

Swift adds a special initializer to CGRect that does just that.

let rect = CGRect(
    origin: CGPoint(x: 0, y: 0),
    size: UIScreen.mainScreen().bounds.size
)

For Swift 3.0 you would do this...

let rect = CGRect(
    origin: CGPoint(x: 0, y: 0),
    size: UIScreen.main.bounds.size
)