Create CGRect with short way - swift
Asked Answered
L

3

16

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?

Lobbyist answered 28/8, 2014 at 20:7 Comment(0)
B
36

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
)
Bolden answered 28/8, 2014 at 20:10 Comment(0)
L
2

Another way for 0,0

let rect = CGRect(
    origin: CGPointZero,
    size: UIScreen.main.bounds.size
)
Lobbyist answered 11/4, 2017 at 9:31 Comment(1)
Swift 3 and above: use CGPoint.zeroAcquittance
O
0
  • swift5

let rect = UIScreen.main.bounds

Olivette answered 30/4, 2019 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.