swift中UIWindow的使用

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
        }   
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...