【问题标题】:Manually Created UIWindow is Wrong Size手动创建的 UIWindow 大小错误
【发布时间】:2016-12-13 13:13:25
【问题描述】:

我正在学习如何在没有 Interface Builder 的情况下创建 iOS 应用程序(即故事板、xib 等)。下面是我正在使用的基本应用程序委托类。问题是当显示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
  }
}

【问题讨论】:

    标签: ios swift uikit uiwindow uiscreen


    【解决方案1】:

    问题原来是我没有启动图像或故事板。出于某种原因,如果没有这个,应用程序默认为 3.5" 大小。感谢此评论:A launch storyboard or xib must be provided unless the app requires full screen

    【讨论】:

    • 引用 Matt Neuberg 的书 "iOS 9 Programming Fundamentals with Swift"(了不起的技术作家!):在您的 Info.plist 中存在 Launch screen interface file base name 键告诉系统您的应用程序在较新的设备类型上原生运行,例如 iPhone 6 和 iPhone 6 Plus。没有它,您的应用将被放大显示……您将无法获得您有权获得的所有像素……
    【解决方案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 对象,我得到了同样的东西。我尝试了您的示例代码只是为了排除任何奇怪之处,它看起来一样。
    • @macinjosh 也许你应该在一个新的纯项目中试用示例代码。您还删除了 main.storyboard 文件吗?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 2012-10-28
      • 1970-01-01
      相关资源
      最近更新 更多