【问题标题】:UIWebView Transparent background in ios8ios8中的UIWebView透明背景
【发布时间】:2014-11-08 16:03:31
【问题描述】:

我正在使用 UIWebview,但在 ios8 中的 web 视图后面有一些黑色背景。在 ios7 中同样可以正常工作。我还将 opaque 设置为 NO。

【问题讨论】:

标签: ios xcode uiwebview ios8


【解决方案1】:

尝试以编程方式将 UIWebview 添加为子视图。

    WebView = [[UIWebView alloc]initWithFrame:CGRectMake(264, 10, 745, 576)];
    WebView.userInteractionEnabled = YES;
    [self setBorderToView: WebView];
    WebView.scrollView.bounces = NO;
    [WebView setBackgroundColor:[UIColor clearColor]];
    [WebView setOpaque:NO];
    [WebView loadHTMLString: embedHTML baseURL: nil];
    [self.view addSubview: WebView];

希望对你有帮助!

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,在没有得到正确的解决方案后,我使用了 Masking 做了一个技巧。

            CALayer *aLayer = [CALayer layer];
            aLayer.backgroundColor = [UIColor blackColor].CGColor;
            aLayer.frame = CGRectMake(origin.x, origin.y, widthOfVideo, heightOfVideo);
            objWebView.layer.mask = aLayer;
    

    这对我有用。希望对您有所帮助。

    【讨论】:

      【解决方案3】:
      Put a instance variable in the in the header file or your class:
      
      // instance variable for interval timer
      NSTimer *BackgroundIntervalTimer;
      Put these methods in the implementation file:
      
      - (void)startUIWebViewBackgroundFixTimer {
          // if > iOS 8 start fixing background color of UIWebPDFView
          if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) {
              // hide pdfWebView until background fixed
              self.pdfWebView.alpha = 0.0;
              // start interval timer
              BackgroundIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:0.1  target:self selector:@selector(fixBackground) userInfo:nil repeats:YES];
          }
      }
      
      - (void)stopUIWebViewBackgroundFixTimer {
          [BackgroundIntervalTimer invalidate];
          BackgroundIntervalTimer = nil;
      }
      
      - (void)fixBackground {
          // stop timer interval
          [self stopUIWebViewBackgroundFixTimer];
      
          // Assuming self.webView is our UIWebView
          // We go though all sub views of the UIWebView and set their backgroundColor to white
          UIView *v = self.pdfWebView;
          while (v) {
              //v.backgroundColor = [UIColor whiteColor];
              v = [v.subviews firstObject];
      
              if ([NSStringFromClass([v class]) isEqualToString:@"UIWebPDFView"]) {
                  [v setBackgroundColor:[UIColor whiteColor]];
      
                  // background set to white so fade view in and exit
                  [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                                   animations:^{
                                       self.pdfWebView.alpha = 1.0;
                                   }
                                   completion:nil];
                  return;
              }
          }
          // UIWebPDFView doesnt exist yet so exit and try later
          [self startUIWebViewBackgroundFixTimer];
      }
      Now put this line right after the line where you load the pdf:
      
      // if iOS 8 check if pdfWebView got subview of class UIWebPDFView
      [self startUIWebViewBackgroundFixTimer];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 2014-09-21
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 2011-03-08
        • 2015-03-26
        相关资源
        最近更新 更多