swift中UIWindow的使用

本文介绍了Swift中UIWindow的基本概念,它作为UIView的子类,负责管理和协调应用中的视图,并处理事件分发。每个iOS应用至少有一个UIWindow,通常用于展示APP内容。内容中提到了UIWindow的显示优先级以及控制keyWindow显示和隐藏的方法,包括makeKeyAndVisible、resignKeyWindow、hidden属性的使用。此外,还提供了源码示例。

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值