在ios 9.1中使用什么代替UIScreen.mainScreen().applicationFrame for swift?

use*_*924 11 iphone xcode ios swift

这可能是一个简单的问题,但由于我是初学者,最好问一下.

由于标题说我应该使用什么而不是 UIScreen.mainScreen().applicationFrame因为它在9.0中被弃用.

如果可能的话,如果您可以提供样品或示例,那将是很好的,因为我发现苹果文档很难.

目前我使用下面的代码,但我希望将其更改为新版本.我很乐意听到你的消息!

  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))
Run Code Online (Sandbox Code Playgroud)

VRA*_*ome 14

使用

let rect1 = UIScreen.mainScreen().bounds // Returns CGRect
let rect2 = UIScreen.mainScreen().bounds.size // Returns CGSize
Run Code Online (Sandbox Code Playgroud)


Ste*_*nye 7

我知道这UIScreen.mainScreen().bounds是推荐的选择; 但是,它并不完全相同UIScreen.mainScreen().applicationFrame,特别是当您的应用程序不占用整个屏幕时,某些iPad上的分割视图和幻灯片功能会发生这种情况.

在这种情况下,这是一个很好的选择: UIApplication.sharedApplication.keyWindow.frame.(当然keyWindow必须是可用的,也就是说,你确实调用了makeKeyAndVisible)