The Window

作者: Matcha00 | 来源:发表于2016-02-29 21:53 被阅读42次

window 创建与加载

  • view 最高层级是window, UIwindow是UIView的子类
  • app 没有sb

创建

main windows 通过 Info.plist的 key “Main storyboard
file base name” ( UIMainStoryboardFile )加载Window
同时设置还window的delegate 属性 所有操作都在这个代理属性之前didFinishLaunchingWithOptions: 完成

加载

lazy var windows : UiWindow? ={
return MyWindow()
}

创建main windows

  • app 没有sb
    1.点击target 找到In the General pane, 选中 “Main”并且删除
    2.删除 Main.storyboard 和 ViewController.swift 从工程中
    3.删除 AppDelegate.swift.
    4.通过代码加载window

import UIKit
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {
self.window = UIWindow()
self.window!.rootViewController = UIViewController()
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
}

加载 main window

self.window = MyWindow()

获取window实例方式

  • 如果一个UIView在界面上,那么 window property 指向window实例也可以用这种方法检验 UIView是否存在

let w = UIApplication.sharedApplication().delegate!.window!!
let w = (UIApplication.sharedApplication().delegate as! AppDelegate).window!
let w = UIApplication.sharedApplication().keyWindow!这种方法系统会创建临时window 把它作为application’s key window

相关文章

网友评论

    本文标题:The Window

    本文链接:https://www.haomeiwen.com/subject/ihvgkttx.html