UIScrollView + UIWebView =没有scrollsToTop [英] UIScrollView + UIWebView = NO scrollsToTop

查看:136
本文介绍了UIScrollView + UIWebView =没有scrollsToTop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像一个可以有简单解决方案的问题,但我没有找到任何可能导致它的方法。我在UIScrollView中使用UIWebView并点击statusBar(将内容滚动到顶部)不起作用。

It looks like a problem which could have simple solution, but I haven't found anything what could lead the way to it. I'm using UIWebView inside of UIScrollView and tapping on statusBar (to scroll content to top) is not working.

我做了一个简单的测试应用程序,看看它是否真的UIWebViews错误。它确实是。

I've made simple test application to see if it's really UIWebViews fault. And it really is.

//  scrolls to top on status bar tap 
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(0, 0, 320, 480);
sv.contentSize = CGSizeMake(320, 1200);
[self.view addSubview:sv];







// doesn't scroll
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(0, 0, 320, 480);
sv.contentSize = CGSizeMake(320, 1200);

UIWebView *wv = [[UIWebView alloc] init];
wv.frame = CGRectMake(0, 0, 320, 100);
[sv addSubview:wv]; 

[self.view addSubview:sv];

所以,我想也许我可以禁用一些东西让UIWebView不要乱用scrollToTop?或者某种解决方法也不错。

So, I think maybe there's something I could disable to make UIWebView not to mess with scrollToTop? Or some kind of workaround also would be nice.

任何想法?

推荐答案

我昨晚遇到了这个问题,直到我终于在评论评论中找到了答案。 原始帖子的想法是当你将UIWebView添加到你的UIScrollingView时,你使用以下内容:

I ran into this last night until I finally found the answer buried in a comment to a comment. The idea of that original post is that when you add the UIWebView to your UIScrollingView, you use the following:

- (void) ensureScrollsToTop: (UIView*) ensureView {
    ((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = NO;
}

这对我来说似乎很可疑,因为UIWebView的第一个子视图声称是一个UIScroller,它不是UIScrollView的子类。但是,由于UIScroller支持scrollsToTop属性,强制转换只是让我们超越编译器警告:

This seemed fishy to me since the first sub-view of a UIWebView claims to be a UIScroller which is not a subclass of UIScrollView. However, since UIScroller supports the scrollsToTop property, the cast just gives us a way past the compiler warning:


Class List:
    Class = UIScroller
    Class = UIView
    Class = UIResponder
    Class = NSObject
Supported Methods:
    ...
    Method _scrollToTop
    Method setScrollsToTop:
    Method scrollsToTop
    ...
Supported Properties:
    Property scrollsToTop

编辑:
关于实际需要发生的地方的另一个快速说明:在webViewDidFinishLoad回调中。在UIWebView构造上调用它是不够好的,因为当时UIWebView还没有创建它的子视图,这导致了问题:

Just another quick note about where this actually needs to occur: in the webViewDidFinishLoad callback. Calling it on UIWebView construction isn't good enough because at that time the UIWebView hasn't created it's child views yet, which are the ones causing the problem:

- (void)webViewDidFinishLoad:(UIWebView *) wv {    
    [self ensureScrollsToTop: wv];
}

编辑#2:

现在,在iOS 5中,如 iPhone操作系统中所述:点击状态栏滚动到顶部在删除/添加后无效返回使用UIWebView的新@property scrollView,对于未使用低于iOS 5.0的部署目标的项目,不需要实现 ensureScrollsToTop

now, in iOS 5, as noted in iPhone OS: Tap status bar to scroll to top doesn't work after remove/add back use UIWebView's new @property scrollView, making ensureScrollsToTop implementation unnecessary for projects that aren't using deployment targets lower than iOS 5.0 .

这篇关于UIScrollView + UIWebView =没有scrollsToTop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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