iOS 半透明的presentVC & 半透明的webView

有很多情況需要模態(tài)出來的viewController是半透明的,還有一些情況是要求webView是半透明的,下面將總結(jié)的經(jīng)驗(yàn)展示:

  1. 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.讀入一個(gè)HTML,直接寫入一個(gè)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設(shè)置半透明
    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. 模態(tài)出來的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>?“ ?≤? ‘??o?</div>
</body>
</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,069評(píng)論 4 61
  • 每天一句:永遠(yuǎn)不要夢(mèng)想一步登天,不要做浮躁的人,不要覺得路途漫長(zhǎng)。而是要編程編程再編程,完了再編程,再編程。 一、...
    EndEvent閱讀 460評(píng)論 0 5
  • int const * const p; 相對(duì)于 * 號(hào)1、const 放左邊表示 指針變量指向常量;2、cons...
    那一年的北海閱讀 343評(píng)論 0 0
  • 我的思念翻山越嶺, 只想飛向你的心田; 我的愛穿越時(shí)空, 只想落入你的眼眸。 我對(duì)你的思念漫天過海, 無止無休; ...
    周茉莉閱讀 449評(píng)論 0 1

友情鏈接更多精彩內(nèi)容