强制UIWebView重绘? [英] Force UIWebView to redraw?

查看:75
本文介绍了强制UIWebView重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何技术可以使UIWebView重绘?我已经在UIWebView及其UIScrollView上尝试过 setNeedsDisplay setNeedsLayout ,但是都没有用.

解决方案

在询问后立即找到答案.关键是要告诉 UIWebView scrollView 的子视图重新绘制自己,特别是 UIWebBrowserView .

 -(void)forceRedrawInWebView:(UIWebView *)webView {NSArray *视图= webView.scrollView.subviews;for(int i = 0; i< views.count; i ++){UIView * view = views [i];//if([NSStringFromClass([view class])isEqualToString:@"UIWebBrowserView"]){[view setNeedsDisplayInRect:webView.bounds];//Webkit Repaint,通常很快[view setNeedsLayout];//Webkit Relayout(比重绘慢)//导致重绘&*整个UIWebView的重传,在屏幕上和关闭时,通常是密集的[view setNeedsDisplay];[view setNeedsLayout];//休息;//在if语句的情况下使用玻璃杯(感谢Jake)//}}} 

为了安全起见,我注释掉了if语句,避免依赖于 UIWebBrowserView 的类名不变.没有它,它将击中滚动视图中的所有UIView,这在当时并不是真正的问题(不会产生大量开销),但是可以随时更改.

在某些情况下,以下JavaScript代码段将完成相同/相似的事情:

  window.scrollBy(1,1);window.scrollBy(-1,-1); 

您会认为 UIScrollView contentOffset 也可以做到这一点,但是根据我的经验,情况并非总是如此-出于某种原因 window.scrollTo 在这方面很特殊.

要点: https://gist.github.com/matt-curtis/5843862

Are there any techniques to cause a UIWebView to redraw itself? I've tried setNeedsDisplay and setNeedsLayout on the UIWebView and its UIScrollView, but neither have worked.

解决方案

Literally found the answer right after asking. The key was to tell the subviews of UIWebView's scrollView to redraw themselves - particularly the UIWebBrowserView.

- (void) forceRedrawInWebView:(UIWebView*)webView {
    NSArray *views = webView.scrollView.subviews;

    for(int i = 0; i<views.count; i++){
        UIView *view = views[i];

        //if([NSStringFromClass([view class]) isEqualToString:@"UIWebBrowserView"]){
            [view setNeedsDisplayInRect:webView.bounds]; // Webkit Repaint, usually fast
            [view setNeedsLayout]; // Webkit Relayout (slower than repaint)

            // Causes redraw & relayout of *entire* UIWebView, onscreen and off, usually intensive
            [view setNeedsDisplay]; 
            [view setNeedsLayout];
            // break; // glass in case of if statement (thanks Jake)
        //}
    }
}

I've commented out the if statement to be safe and avoid reliance on UIWebBrowserView's class name not changing. Without it, it hits all UIViews that are in the scrollview, which isn't really a problem at this point (no significant overhead incurred) but could always change.

EDIT:

In some cases, the following snippet of JavaScript will accomplish the same/similar thing:

window.scrollBy(1, 1); window.scrollBy(-1, -1);

You'd think UIScrollView's contentOffset would do this too, but that's not always the case in my experience - for some reason window.scrollTo is special in this regard.

Gist: https://gist.github.com/matt-curtis/5843862

这篇关于强制UIWebView重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆