用UIWindow作为弹出视图的遮罩层

关键点:
1、要把要显示的window定义为一个static变量。
   这样,它不会在方法结束后被销毁。
2、不需要将window add 到某个view或window上去。

    只要alloc了一个window,并且window.hiden = NO,则这个window就会显示了。


static UIWindow *__sheetWindow = nil;


@implementation ViewController


-(void)addWindowAction{

    

    UIWindow *window = [[UIWindow alloc] initWithFrame:(CGRect) {{0.f, 0.f}, [[UIScreen mainScreen] bounds].size}];

    window.backgroundColor = [UIColor clearColor];

    window.windowLevel = UIWindowLevelNormal;

    window.alpha = 1.f;

    window.hidden = NO;

    

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];

    UIButton* btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    btn.frame = CGRectMake(10, 10, 20, 20);

    [view addSubview:btn];

    [btn addTarget:self action:@selector(removeWindowAction) forControlEvents:UIControlEventTouchUpInside];

    view.backgroundColor = [UIColor redColor];

    [window addSubview:view];

    

    __sheetWindow = window;

}


-(void)removeWindowAction{

    __sheetWindow.hidden = YES;

    __sheetWindow = nil;

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值