美文网首页iOS
商品详情

商品详情

作者: CYC666 | 来源:发表于2019-10-08 13:41 被阅读0次

1、加载本地H5

2、自适应宽高

代码

    // 详情

    {

        //创建网页配置对象

        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];

        // 创建设置对象

        WKPreferences *preference = [[WKPreferences alloc]init];

        //最小字体大小 当将javaScriptEnabled属性设置为NO时,可以看到明显的效果

        preference.minimumFontSize=0;

        //设置是否支持javaScript 默认是支持的

        preference.javaScriptEnabled=YES;

        // 在iOS上默认为NO,表示是否允许不经过用户交互由javaScript自动打开窗口

        preference.javaScriptCanOpenWindowsAutomatically = YES;

        config.preferences= preference;

        // 是使用h5的视频播放器在线播放, 还是使用原生播放器全屏播放

        config.allowsInlineMediaPlayback = YES;

        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放

        config.requiresUserActionForMediaPlayback = YES;

        //设置是否允许画中画技术 在特定设备上有效

        config.allowsPictureInPictureMediaPlayback = YES;

        NSString *htmlString = [NSString stringWithFormat:@"<html> \n"

                                "<head> \n"

                                "<style type=\"text/css\"> \n"

                                "body {font-size:15px;}\n"

                                "</style> \n"

                                "</head> \n"

                                "<body>"

                                ""

                                "window.onload = function(){\n"

                                "var $img = document.getElementsByTagName('img');\n"

                                "for(var p in  $img){\n"

                                " $img[p].style.width = '100%%';\n"

                                "$img[p].style.height ='auto'\n"

                                "}\n"

                                "}"

                                "</script>%@"

                                "</body>"

                                "</html>",_resultDic[@"goods_info"][@"mobile_body"]];

        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, viewHeight, SCREEN_WIDTH, 600) configuration:config];

        _webView.allowsBackForwardNavigationGestures = YES;

        _webView.navigationDelegate = self;

        [_webViewloadHTMLString:htmlStringbaseURL:[NSURLURLWithString:htmlString]];

        [viewaddSubview:_webView];

    }

#pragma mark ========================================代理方法=============================================

- (void)webView:(WKWebView*)webViewdidFinishNavigation:(null_unspecifiedWKNavigation*)navigation {

    [webViewevaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {

        NSNumber*height = result;

        _webHeight= height.floatValue;

        [self updateWebUIAction];

    }];

    [webViewevaluateJavaScript:@"document.body.scrollWidth" completionHandler:^(id _Nullable result,NSError * _Nullable error) {

        NSNumber*width = result;

        _webWidth= width.floatValue;

        [self updateWebUIAction];

    }];

}

#pragma mark- 刷新文网页UI

- (void)updateWebUIAction {

    if(_webHeight>0&&_webWidth>0) {

        CGFloatheight =SCREEN_WIDTH*_webHeight/_webWidth;

        _webView.frame=CGRectMake(0,_mainHeight,SCREEN_WIDTH, height);

        UIScrollView *view = (UIScrollView *)_webView.superview;

        view.contentSize=CGSizeMake(SCREEN_WIDTH,_mainHeight+ height);

    }

}

相关文章

  • 商品详情

    4个链接 1. 公司介绍:让阳光照亮您的黑夜(亚马逊是否可以展示?) :研发部门、生产车间、品质监管部、销售部等各...

  • 商品详情

    1、加载本地H5 2、自适应宽高 代码 // 详情 { //创建网页配置对象 WKWebV...

  • 商品详情

    商品详情页面 主要结构,商品图片的轮播图,商品购买区域,商品参数区域 使用mui-card样式构建商品详情界面的三...

  • 商品详情

    商品详情 简介 商品详情是一个高并发访问的功能,需要考虑缓存、性能和机器压力京东的商品详情: 一个请求过去,所有的...

  • 电商平台 商品,详情 ,评价 运用主子路由三者切换

    html