如圖我們會(huì)經(jīng)常遇到這種需要點(diǎn)擊彈出視圖的效果绒北,利用UIWindow能夠比較容易地實(shí)現(xiàn)這個(gè)效果厨幻。
首先我們要注意以下兩點(diǎn):
1、定義一個(gè)static變量用來(lái)保存要顯示的window毒姨,避免方法結(jié)束后被銷毀冤竹。
2、不需要將window add 到某個(gè)view或window上去案铺。
只要alloc了一個(gè)window,并且window.hiden = NO,則這個(gè)window就會(huì)顯示了蔬芥。
在用戶點(diǎn)擊《平臺(tái)用戶協(xié)議》時(shí)彈出協(xié)議具體內(nèi)容
static UIWindow *myWindow = nil;
//用戶協(xié)議
- (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)];
//創(chuàng)建URL
NSURL* url = [NSURL URLWithString:@"http://192.168.0.117:8080/LifeService/contract/app/tre.do"];
//創(chuàng)建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;
}
//關(guān)閉彈窗的方法
-(void)removeWindowAction{
myWindow.hidden = YES;
mytWindow = nil;
}