zoukankan      html  css  js  c++  java
  • ios- webView和WKWebView图片超出边界处理办法

    https://www.jianshu.com/p/5331aef351e0   

    ios- webView和WKWebView图片超出边界处理办法

    代码:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

        

        self.titleLabel.text = self.model.name;

        

        self.myScrollView.delegate = self;

        

        self.backBtnTopLayout.constant = StatusBarHeight;

        self.backBtnHeightLayout.constant = self.navigationBarHeight;

        

        self.bannerView.autoScrollTimeInterval = 5;

        self.bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;

        self.bannerView.backgroundColor = [UIColor whiteColor];

        

        // WKWebView的配置

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

        // Webview的偏好设置

        configuration.preferences = [[WKPreferences alloc] init];

        configuration.preferences.minimumFontSize = 10;

        configuration.preferences.javaScriptEnabled = YES;

        // 默认是不能通过JS自动打开窗口的,必须通过用户交互才能打开

        configuration.preferences.javaScriptCanOpenWindowsAutomatically = NO;

        

        //设置网页自适应屏幕宽度

        NSString *jScript = @"`var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta)`;";

          

        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart|WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

        WKUserContentController *wkUController = [[WKUserContentController alloc] init];

        [wkUController addUserScript:wkUScript];

        configuration.userContentController = wkUController;

        

        

        

        self.myWebView = [[WKWebView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.detailLabel.frame), CGRectGetMaxY(self.detailLabel.frame)+ValueOfScaleHeight(23), CGRectGetWidth(self.detailLabel.frame)+ValueOfScaleHeight(5), SWidth) configuration:configuration];

        self.myWebView.backgroundColor = [UIColor whiteColor];

        self.myWebView.navigationDelegate = self;

        self.myWebView.scrollView.scrollEnabled = NO;

        self.myWebView.clipsToBounds = YES;

        [self.detailLabel.superview addSubview:self.myWebView];

        

        self.bannerView.imageURLStringsGroup = self.model.imageList;

        self.nameLabel.text = self.model.name;

        if (([self.model.price doubleValue]==0)) {

            self.priceLabel.text = [NSString stringWithFormat:@"%ld积分", (long)self.model.integral];

        } else if (self.model.integral==0) {

               self.priceLabel.text = [NSString stringWithFormat:@"¥%@", self.model.price];

        } else {

            self.priceLabel.text = [NSString stringWithFormat:@"¥%@+%ld积分", self.model.price, (long)self.model.integral];

        }

        

        [self.myWebView loadHTMLString:self.model.detail baseURL:nil];

        NSString *js = @"function imgAutoFit() {

           var imgs = document.getElementsByTagName('img');

           for (var i = 0; i < imgs.length; ++i) {

              var img = imgs[i];  

              img.style.maxWidth = %f;  

           }

        }";

        js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width];

        //注入js 到html中

        [self.myWebView evaluateJavaScript:js completionHandler:nil];

        

        //调用

        [self.myWebView evaluateJavaScript:@"imgAutoFit()" completionHandler:nil];

        

    }

    #pragma mark - wk navigation delegate

    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {

        

    //    NSString *js = @"function imgAutoFit() {

    //       var imgs = document.getElementsByTagName('img');

    //       for (var i = 0; i < imgs.length; ++i) {

    //          var img = imgs[i];  

    //          img.style.maxWidth = %f;  

    //       }

    //    }";

    //    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width + 20];

    //

    //    //注入js 到html中

    //    [self.myWebView evaluateJavaScript:js completionHandler:nil];

    //

    //    //调用

    //    [self.myWebView evaluateJavaScript:@"imgAutoFit()" completionHandler:nil];

        //获取webView的高度

        DefineWeakSelf;

        [self.myWebView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {

            CGRect webViewFrame = weakSelf.myWebView.frame;

            webViewFrame.size.height = [result doubleValue];

            weakSelf.myWebView.frame = webViewFrame;

            weakSelf.detailViewHeightLayout.constant = CGRectGetMaxY(weakSelf.myWebView.frame)+ValueOfScaleHeight(21);

            

            

            AFTER(0.5, ^{

                weakSelf.myScrollView.contentSize = CGSizeMake(SWidth, CGRectGetMaxY(weakSelf.myWebView.superview.frame));

            });

        }];

    }

    #pragma mark - scroll view delegate

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

        double topHeight = CGRectGetMinY(self.nameLabel.superview.frame);

        if (scrollView.contentOffset.y>=topHeight) {

            self.navigationBar.alpha = 1;

            self.backBtn.alpha = 0;

        } else {

            self.navigationBar.alpha = scrollView.contentOffset.y/topHeight;

            self.backBtn.alpha = 1-self.navigationBar.alpha;

        }

    }

    #pragma mark - 返回

    - (IBAction)backBtnClicked:(UIButton *)sender {

        [QMHelper popViewController];

    }

    #pragma mark - 分享

    - (IBAction)shareBtnClicked:(UIButton *)sender {

    }

    #pragma mark - 兑换

    - (IBAction)replaceBtnClicked:(UIButton *)sender {

        [QMHelper pushWithController:[[BHIntegralReplaceVC alloc] initWithModel:self.model]];

    }

  • 相关阅读:
    LSMW TIPS
    Schedule agreement and Delfor
    Running VL10 in the background 13 Oct
    analyse idoc by creation date
    New Journey Prepare
    EDI error
    CBSN NEWS
    Listen and Write 18th Feb 2019
    Microsoft iSCSI Software Target 快照管理
    通过 Microsoft iSCSI Software Target 提供存储服务
  • 原文地址:https://www.cnblogs.com/supersr/p/11906138.html
Copyright © 2011-2022 走看看