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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33449696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:08:56  来源:igfitidea点击:

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

iosiphonexcodeswift

提问by Jennifer

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().applicationFramesince it's deprecated in 9.0.

正如标题所说,我应该使用什么来代替, UIScreen.mainScreen().applicationFrame因为它在 9.0.0 中已被弃用。

If possible it would be great if you can provide me with a sample or example ,since I find Apples document difficult.

如果可能的话,如果您能向我提供样本或示例,那就太好了,因为我发现 Apples 文档很困难。

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))

回答by VRAwesome

Use

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

回答by Stephenye

I know that UIScreen.mainScreen().boundsis 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.

我知道这UIScreen.mainScreen().bounds是推荐的替代方案;但是,它并不完全等同于UIScreen.mainScreen().applicationFrame,尤其是当您的应用程序不占据整个屏幕时,某些 iPad 上的分屏浏览和侧滑功能会发生这种情况。

In that case, this is a good alternative: UIApplication.sharedApplication.keyWindow.frame. (of course keyWindowhas to be available, that is, you did invoke makeKeyAndVisible)

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

回答by Sam B

As of swift 3 you need to use it like this now.

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

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

回答by Orkhan Alizade

For:

为了:

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

and for:

和:

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