ios - 为什么我们需要在iOS中设置keywindow

标签 ios swift

Whereas touch events are delivered to the window where they occurred, events that do not have a relevant coordinate value are delivered to the key window. Only one window at a time can be the key window, and you can use a window’s isKeyWindow property to determine its status. Most of the time, your app’s main window is the key window, but UIKit may designate a different window as needed. https://developer.apple.com/documentation/uikit/uiwindow

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

  // Initialize the window
  window = UIWindow.init(frame: UIScreen.mainScreen().bounds)

  // Set Background Color of window
  window?.backgroundColor = UIColor.whiteColor()

  // Allocate memory for an instance of the 'MainViewController' class
  let mainViewController = MainViewController()

  // Set the root view controller of the app's window
  window!.rootViewController = mainViewController

  // Make the window visible
  window!.makeKeyAndVisible()

  return true
}  

我读了但还是不明白为什么我们需要将窗口设置为键窗口。

Whereas touch events are delivered to the window where they occurred, events that do not have a relevant coordinate value are delivered to the key window

将窗口设置为键有什么作用,不是之后会发生什么?

谢谢

最佳答案

  • makeKeyAndVisible 消息生成一个窗口键,并将其移动到 在其级别的任何其他窗口前。

  • 调用 makeKeyAndVisible 用于指定哪个是当前的。

  • 它将新的 View Controller 分配给窗口的 rootViewController 属性,然后使窗口在屏幕上可见。

    - 这是一种显示当前窗口并将其放置在同一级别或更低级别的所有其他窗口前面的便捷方法。如果你只想显示窗口,改变它 隐 属性(property)给 没有 . see Apple Doc

关于ios - 为什么我们需要在iOS中设置keywindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643787/

相关文章:

ios - WWDC 2019 - iOS13 2020 年 4 月要求

ios - Ford SYNC Applink™ 仿真器的 iPhone 连接

ios - Xcode Swift Collection View : How can I add the number of items in section whenever the button is tapped?

ios - Swift:将注释标题传递给我的第二个 View Controller 标签

ios - 从数组中提取 URL 字符串并放入 IBAction

swift - SKShapeNode 在我不想的时候与我的 SKPhysicsbody 发生碰撞

objective-c - 有放回加权随机抽样的高效算法

ios - 无法快速使用 Protocol 扩展 ObjC 类

ios - 如何在 xcode 中编写支持所有尺寸 iphone 的代码

ios - 在 Swift 中传递数组