美文网首页
iOS WKWebView 内容超出屏幕后不显示超出部分的内容

iOS WKWebView 内容超出屏幕后不显示超出部分的内容

作者: LoveBe | 来源:发表于2022-10-23 14:30 被阅读0次
    NSString *head = @"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> <style>img{max-width: 100%; width:100%; height:auto;}*{margin:0px;}</style></head>";
    content = [NSString stringWithFormat:@"<html>%@<body>%@</body></html>",head,content];// 超出屏幕的部分不显示
    
    /// 适配内容字体不会变小,禁止内容放大缩小,禁止内容左右弹性属性
    NSString *jScript = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
        
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
        
    contentWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
    contentWebView.navigationDelegate = self;
    contentWebView.UIDelegate = self;
    ///修复在cell上滑动html内容出现卡顿
    contentWebView.scrollView.bounces = NO;
    ///为YES时,超出屏幕宽度可滑动
    contentWebView.scrollView.scrollEnabled = NO;
    [cell.contentView addSubview:contentWebView];
    [contentWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.offset(10);
        make.right.offset(-10);
    }];
    [contentWebView loadHTMLString:content baseURL:nil];
    

    相关文章

      网友评论

          本文标题:iOS WKWebView 内容超出屏幕后不显示超出部分的内容

          本文链接:https://www.haomeiwen.com/subject/buxgzrtx.html