使用UIWindow

UIWindow并不包含任何默认的内容,但是它被当作UIView的容器,用于放置应用中所有的UIView。UIWindow更多的时候只作为UIView的顶层容器存在。
而我们最常用的方法,就是在程序刚启动时,调用UIWindow的makeAndVisible方法,代码如下:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}```
我们可以在UIWindow做一些操作:

// 取得最上面的window self代表一个view
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
[window addSubview:self];

WindowLevel属性
不是所有创建的UIWindow一定会覆盖在界面的最上面,UIWindow有一个类型为“UIWindowLevel”的属性,该属性定义了UIWindow的层级,系统定义的UIWindowLevel一共有3中取值:如下:

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar

程序中默认的UIWindow的层级是UIWindowLevelNormal。

手动创建UIWindow

//创建UIWindown
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.windowLevel = UIWindowLevelNormal;
_window.backgroundColor = [UIColor blueColor];
_window.hidden = NO;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideUIWindown)];
[_window addGestureRecognizer:tap];

/**
隐藏UIWindown
*/

  • (void)hideUIWindown{
    _window.hidden = YES;
    _window = nil;
    }
在一个应用程序中,想做一个密码保护功能,在用户从应用的任何界面按Home键退出,过一段时间再从后台切换回来,显示一个密码输入界面。只有用户在输入正确的密码,才能够进入退出的界面。因为这个密码输入界面可能从任何应用界面弹出,并且需要盖在所有界面的最上层,所以它很适合用一个UIWindow来实现。

import "PasswordInputwindow.h"

@implementation PasswordInputwindow

  • (instancetype)shareInstance
    {
    static id shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    shareInstance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return shareInstance;
    }
  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    self.rootViewController = vc;

      UITextField *textfield = [[UITextField alloc] init];
      textfield.textColor = [UIColor blackColor];
      textfield.font = [UIFont systemFontOfSize:17];
      textfield.frame = CGRectMake(20, 120, [UIScreen mainScreen].bounds.size.width-40, 40);
      textfield.borderStyle = UITextBorderStyleRoundedRect;
      textfield.placeholder = @"在这里输入密码";
      [vc.view addSubview:textfield];
      
      UIButton *btn = [[UIButton alloc] init];
      [btn setTitle:@"隐藏界面" forState:UIControlStateNormal];
      [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      btn.titleLabel.font = [UIFont systemFontOfSize:17];
      [btn sizeToFit];
      btn.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
      [btn addTarget:self action:@selector(hideWindown:) forControlEvents:UIControlEventTouchUpInside];
      [vc.view addSubview:btn];
    

    }

    return self;
    }

  • (void)hideWindown:(UIButton *)sender{
    [self resignKeyWindow];
    self.hidden = YES;
    }

  • (void)show
    {
    [self makeKeyWindow];
    self.hidden = NO;
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {

    [[PasswordInputwindow shareInstance] show];
    }

如果我们创建的UIWindow需要处理键盘事件,那么就需要合理的将其设置为keywindow。keywindow是被系统设计用来接受键盘和其他非触摸事件的UIWindow。我们可以通过makeKeyWindow和makeKeyAndVisible方法来将自己创建的UIWindow实例设置为keywindow。支付宝客户端的手势解锁功能,就是UIWindow的一个极好的应用。


/***  文中的知识摘自“iOS开发进阶”   ****/

你可能感兴趣的:(使用UIWindow)