Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CGRect with short way - swift

Tags:

swift

cgrect

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?

like image 929
xarly Avatar asked Aug 28 '14 20:08

xarly


1 Answers

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
)
like image 100
Mick MacCallum Avatar answered Sep 18 '22 15:09

Mick MacCallum