xarly
xarly

Reputation: 2144

Create CGRect with short way - swift

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?

Upvotes: 16

Views: 23120

Answers (3)

AllenPong
AllenPong

Reputation: 15

  • swift5

let rect = UIScreen.main.bounds

Upvotes: 0

xarly
xarly

Reputation: 2144

Another way for 0,0

let rect = CGRect(
    origin: CGPointZero,
    size: UIScreen.main.bounds.size
)

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130193

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
)

Upvotes: 36

Related Questions