https://github.com/potato512/SYSwiftLearning
UIWindow继承自UIView,用来管理和协调各种视图。提供一个区域来显示视图,将事件event分发给视图。
每个iOS应用必须包含一个window用于展示APP的交互页面, 且一个APP通常只有一个UIWindow,包含了APP的可视内容。
显示优先级,通常会有三个值,优先级顺序为:
UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelnormal
控制keyWindow的显示,隐藏方法
(1)显示:makeKeyAndVisible方法,及hiddin属性
(2)隐藏:resignKeyWindow方法,及hidden属性
效果图:
源码示例:
func alertClick()
{
// UIScreen.mainScreen().bounds
if self.alertwindow == nil
{
self.alertwindow = UIWindow(frame: UIScreen.mainScreen().bounds)
self.alertwindow.windowLevel = UIWindowLevelAlert
self.alertwindow.backgroundColor = UIColor.yellowColor()
self.alertwindow.becomeKeyWindow()
let label = UILabel(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.alertwindow.frame) - 10.0 * 2),180.0))
self.alertwindow.addSubview(label)
label.backgroundColor = UIColor.orangeColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(13.0)
label.text = "UIWindow继承自UIView,用来管理和协调各种视图。提供一个区域来显示视图,将事件event分发给视图。\n每个iOS应用必须包含一个window用于展示APP的交互页面, 且一个APP通常只有一个UIWindow,包含了APP的可视内容。\n显示优先级,通常会有三个值,优先级顺序为:UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelnormal."
let button = UIButton(frame: CGRectMake((CGRectGetWidth(self.alertwindow.frame) - 10.0 - 80.0),(CGRectGetMinX(label.frame) + CGRectGetHeight(label.frame) + 10.0),80.0,30.0))
self.alertwindow.addSubview(button)
button.backgroundColor = UIColor.greenColor()
button.setTitle("hidden",forState: .normal)
button.setTitleColor(UIColor.blackColor(),forState: .normal)
button.setTitleColor(UIColor.redColor(),forState: .Highlighted)
button.addTarget(self,action: Selector("hiddenClick"),forControlEvents: .TouchUpInside)
}
self.alertwindow.makeKeyAndVisible()
self.alertwindow.hidden = false
}
func hiddenClick()
{
if self.alertwindow != nil
{
self.alertwindow.resignKeyWindow()
self.alertwindow.hidden = true
}
}