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

时间:2015-10-31 08:43:00

标签: ios iphone xcode 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))

5 个答案:

答案 0 :(得分:13)

使用

let rect1 = UIScreen.mainScreen().bounds // Returns CGRect
let rect2 = UIScreen.mainScreen().bounds.size // Returns CGSize

答案 1 :(得分:6)

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

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

答案 2 :(得分:3)

有关:

let width = UIScreen.mainScreen().bounds.width

和for:

let height = UIScreen.mainScreen().bounds.height

答案 3 :(得分:2)

从swift 3开始,你现在需要像这样使用它。

let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height

答案 4 :(得分:0)

Swift 3+:

let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height