代码实现如下
#import “ViewController.h”
#import <WebKit/WebKit.h>
@interface ViewController ()
@property (nonatomic,strong)WKWebView * webView;
@end

@implementation ViewController

(void)viewDidLoad {
[super viewDidLoad];

_webView = [[WKWebView alloc] initWithFrame:self.view.bounds];//初始化

[self.view addSubview:_webView];


//1.网络
_webView.allowsBackForwardNavigationGestures = YES;
 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com/"]]; 

[_webView loadRequest:request];



//2.本地html 
 //获取bundlePath 路径
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    
    //获取本地html目录 basePath
    NSString *basePath = [NSString stringWithFormat: @"%@/dist", bundlePath];
    
    //获取本地html目录 baseUrl
    NSURL *baseUrl = [NSURL fileURLWithPath: basePath isDirectory: YES];
    
    NSLog(@"%@", baseUrl);
    //html 路径
    
    NSString *indexPath = [NSString stringWithFormat: @"%@/index.html", basePath];
    //html 文件中内容
    NSString *indexContent = [NSString stringWithContentsOfFile: indexPath encoding: NSUTF8StringEncoding error:nil];
    //显示内容
    [webview loadHTMLString: indexContent baseURL: baseUrl];

}

 WKWebView实现背景透明

@interface 你的页面 ()<WKNavigationDelegate>

 //遵循协议

    _webView.navigationDelegate = self;

//关键一句
self.webView.opaque = NO;
//或者
[self.webView setOpaque:NO];

// 似乎这两句没什么用
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.scrollView.backgroundColor = UIColor.clearColor;

亲测有效

 

相关文章:

  • 2022-12-23
  • 2021-09-19
  • 2022-01-13
  • 2021-04-18
  • 2021-05-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-09
  • 2022-12-23
  • 2022-01-02
相关资源
相似解决方案