手动创建的UIWindow大小有误

11

我正在学习如何创建一个不使用Interface Builder(即故事板、xib等)的iOS应用程序。以下是我正在使用的基本应用委托类。问题在于,当UIWindow被显示时,它没有占满设备屏幕的整个区域(请参见附带的截图)。这种情况发生在我测试的所有设备和模拟器上。为什么不使用全屏?

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  lazy var window: UIWindow? = {
    debugPrint("AppDelegate: Creating Window")

    let screen: UIScreen = UIScreen.mainScreen()
    let window: UIWindow = UIWindow.init(frame: screen.bounds)

    window.backgroundColor = UIColor.whiteColor()

    return window
  }()

  lazy var rootViewController: ShapesViewController = {
    debugPrint("AppDelegate: Creating Root View Controller")

    let rootViewController = ShapesViewController.init(nibName: nil, bundle: nil)

    return rootViewController
  }()

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    debugPrint("AppDelegate: applicationWillEnterForeground")

    window?.rootViewController = rootViewController
    window?.makeKeyAndVisible()

    return true
  }
}

为什么窗口没有使用整个屏幕?

3个回答

15

2
引用Matt Neuberg(令人惊叹的技术作家!)的《iOS 9编程基础与Swift》一书中的内容:在您的Info.plist中存在一个 Launch screen interface file base name 键表明您的应用程序原生地运行在更新的设备类型上,例如iPhone 6和iPhone 6 Plus。如果没有它,您的应用程序将被缩放显示...您将无法获得所有应得的像素... - Basil Bourque

2

这对我总是有效的(尽管我没有看到任何明显的区别)。你介意也分享一下你的ShapesViewController代码吗?

已更新至swift 4.2

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
    return true
}

ShapesViewController是空的。我甚至将根视图控制器更改为普通的UIViewController对象,但结果仍然相同。我尝试了你的示例代码,以排除任何奇怪的问题,但看起来也一样。 - joshwbrick
1
@macinjosh 也许你应该在一个新的空白项目中尝试一下示例代码。另外,你删除了 main.storyboard 文件吗? - David

0

我知道这听起来很傻,但是... 对于一个新项目,macinjosh所说的方法是有效的。 对于我的一个在Xcode 8和Swift 3中的项目,它并不适用(可能是因为我删除了启动屏幕,然后又重新创建了)。 刚刚创建一个全新的项目,按照macinjosh所说的方法进行操作。然后将所有文件迁移回去。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接