xib中如何使用WKWebView
2017-02-27 本文已影响1031人
天天星期天
WKWebView推出有一段时间。公司近来要求所有的uiwebview升级到wkwebview。结果公司的很多界面用的都是xib。所以替换类型发现各种奔溃。废话不多说,直接上解决方法:
1.xib中拉入UIView.(为什么拉入view呢?因为WKWebView的父类是uiview,而xib不能直接拉入WKWebView) ;

2.自定义一个类myWK,继承自wkwebview。(因为wkwebview本身没有实现initWithCoder:方法。这个方法必须实现,否则xib会奔溃)。

实现父类方法
- (instancetype)initWithCoder:(NSCoder *)coder
{
CGRect frame = [[UIScreen mainScreen] bounds];
WKWebViewConfiguration *myConfiguration = [WKWebViewConfiguration new];
self = [super initWithFrame:frame configuration:myConfiguration];
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}
3.替换第一步中xib拉入uiview的class,改为第二步中自定义的类。

4.可以拉线自己调用了。
