UIWindow创建局部弹框

如图我们会经常遇到这种需要点击弹出视图的效果,利用UIWindow能够比较容易地实现这个效果。

首先我们要注意以下两点:
1、定义一个static变量用来保存要显示的window,避免方法结束后被销毁。
2、不需要将window add 到某个view或window上去。
只要alloc了一个window,并且window.hiden = NO,则这个window就会显示了。

在用户点击《平台用户协议》时弹出协议具体内容

static UIWindow *myWindow = nil;
//用户协议
- (IBAction)userProtocolBtnClick:(UIButton *)sender {

UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(30, 80, screenW - 30 * 2, screenH * 0.7)];
window.backgroundColor = [UIColor clearColor];
window.windowLevel = UIWindowLevelNormal;
window.alpha = 1.f;
window.hidden = NO;

UIWebView *webV = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];

//创建URL
NSURL* url = [NSURL URLWithString:@"http://192.168.0.117:8080/LifeService/contract/app/treaty.do"];
//创建NSURLRequest
NSURLRequest* request = [NSURLRequest requestWithURL:url];
//加载
[webV loadRequest:request];

UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(window.frame.size.width - 20, 0, 20, 20);
[btn setImage:[UIImage imageNamed:@"calcelBtn"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(removeWindowAction) forControlEvents:UIControlEventTouchUpInside];

[window addSubview:webV];
[window addSubview:btn];

myWindow = window;
}

//关闭弹窗的方法
-(void)removeWindowAction{
myWindow.hidden = YES;
mytWindow = nil;
}

posted @ 2017-04-22 15:28  估唔到  阅读(361)  评论(0编辑  收藏  举报