有很多情况需要模态出来的viewController是半透明的,还有一些情况是要求webView是半透明的,下面将总结的经验展示:
- ViewController中:
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UIWebViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, 100, 50)];
[button setTitle:@"Button" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
button.backgroundColor = [UIColor grayColor];
[self.view addSubview:button];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 200, 200, 300)];
webView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.5];
// webView.backgroundColor = [UIColor orangeColor];
// [webView setScalesPageToFit:YES];
[webView setOpaque:NO];
webView.delegate = self;
[self.view addSubview:webView];
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://b.bigaka.com/html/cky/ckyhelp.html"]];
// //http://b.bigaka.com/html/cky/ckyhelp.html
// //http://www.baidu.com
// [webView loadRequest:request];
//2.加载本地文件资源
NSString *pathStr = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:pathStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
//3.读入一个HTML,直接写入一个HTML代码
//NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"];
//NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
//[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
}
- (void)clickBtn
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
//present的ViewController设置半透明
secondVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
secondVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2. 模态出来的VC
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.4];
// [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.868f]];
// self.view.backgroundColor = [UIColor whiteColor];
// self.view.backgroundColor = [UIColor clearColor];
// [self.view setOpaque:NO];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
[button setTitle:@"Button" forState:(UIControlStateNormal)];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void)clickBtn
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
3. 添加本地的h5
w02.png代码:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
html{background:rgba(255,255,255,0.5)}
body{background:rgba(255,255,255,0.5)}
</style>
</head>
<body>
<div>Œ“ «≤‚ ‘Œƒº˛</div>
</body>
</html>
网友评论