Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use instead of UIScreen.mainScreen().applicationFrame for swift in ios 9.1?

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))
like image 356
Jennifer Avatar asked Oct 31 '15 08:10

Jennifer


2 Answers

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
like image 175
VRAwesome Avatar answered Oct 10 '22 11:10

VRAwesome


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)

like image 14
Stephenye Avatar answered Oct 10 '22 12:10

Stephenye