This might be a simple question but since I'm a beginner, it is best to ask.
As the title says what should I use instead of UIScreen.mainScreen().applicationFrame
since it's deprecated in 9.0.
If possible it would be great if you can provide me with a sample or example ,since I find Apples document difficult.
Currently I use the code below but I wish to change it to the new version. I would love to hear from you!
let sizeRect = UIScreen.mainScreen().applicationFrame
let posX = arc4random_uniform(UInt32(sizeRect.size.width))
let posY = arc4random_uniform(UInt32(sizeRect.size.height))
Enemy.position = CGPoint(x: CGFloat(posX), y: CGFloat(posY))
Use
let rect1 = UIScreen.mainScreen().bounds // Returns CGRect
let rect2 = UIScreen.mainScreen().bounds.size // Returns CGSize
Swift 3+ :
let rect1 = UIScreen.main.bounds // Returns CGRect
let rect2 = UIScreen.main.bounds.size // Returns CGSize
I know that UIScreen.mainScreen().bounds
is the recommended alternative; however, it's not the exact equivalent to UIScreen.mainScreen().applicationFrame
, particularly when your application does NOT occupy the whole screen, which happens with Split View and Slide Over feature on certain iPads.
In that case, this is a good alternative:
UIApplication.sharedApplication.keyWindow.frame
. (of course keyWindow
has to be available, that is, you did invoke makeKeyAndVisible
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With