【问题标题】:UIWebView scrollView in iOS 4 doesn't call contentOffsetiOS 4 中的 UIWebView scrollView 不调用 contentOffset
【发布时间】:2012-03-13 18:10:49
【问题描述】:

我有以下代码为 UIWebView 的 scrollView 设置 contentInset

webScrollView.contentInset = UIEdgeInsetsMake(44, 0.0, 0.0, 0.0);

这是我获得 UIWebView 滚动视图的方法:

 - (UIScrollView *)defaultScrollView {
        UIScrollView *scrollView = nil;

        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
        if ([currSysVer compare:@"5.0" options:NSNumericSearch] != NSOrderedAscending) {
            return self.scrollView;
        }
        else {
            for (UIView *subview in [self subviews]) {
                if ([subview isKindOfClass:[UIScrollView class]]) {
                    scrollView = (UIScrollView *)subview;
                }
            }

            if (scrollView == nil) {
                NSLog(@"Couldn’t get default scrollview!");
            }
        }
        return scrollView;
    }

在 iOS 5 中调用上述方法也会设置 contentOffset,我知道这一点是因为它调用了 scrollViewDidScroll 委托,但是在 iOS 4 中,它不...知道为什么会这样以及如何解决它?

【问题讨论】:

  • scrollViewDidScroll 被调用,当您设置插图时(在 ios5 中)?我认为这不应该发生..!?
  • 偏移量保持在相同的值,与插入无关。 (例如 0,如果尚未更改)
  • @jaydee3 是的,这是正确的.. 我删除了 setContentInset 并且它没有在 iOS 5 上调用 scrollViewDidScroll .. 但是在 iOS 4 上它在设置内容插入时从不调用 scrollViewDidScroll,这是苹果的错误吗?

标签: iphone objective-c ipad uiwebview uiscrollview


【解决方案1】:

好的,我在 iOS 4 和 iOS 5 的模拟器中对其进行了测试。一旦我设置了插图,代理就会在两个操作系统版本中被调用。

委托的文件告诉我们:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

// Tells the delegate when the user scrolls the content view within the receiver.
// The delegate typically implements this method to obtain the change in
// content offset from scrollView and draw the affected portion of the content view.

首先它说当用户滚动时,事实并非如此。但详细地说,它会在您设置插图时发生内容偏移的更改。因为内容不会改变位置,所以当你设置一个 contentinset 时,它会相应地修正偏移量。

所以这不是错误。但是应该调用委托。在我的测试中 - 确实如此。

好的,我的完整测试:我看到的 4.0 和 5.0 之间的唯一区别是 ScrollView 在 5.0 中是 _UIWebViewScrollView。此外,webview-scrollview 的行为与标准滚动视图不同。在标准的滚动视图中,委托GETS被调用,在webviewScrollView中它没有被调用。

完整的测试代码:

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];    


    NSLog(@"System: %@, iOS %@",
          [UIDevice currentDevice].systemName,
          [UIDevice currentDevice].systemVersion);

    UIWebView* webView = [[UIWebView alloc] initWithFrame: CGRectMake(20, 20, 200, 200)];
    [self.view addSubview: webView];

    UIScrollView* scrollView = [self getScrollViewFromWebView: webView];
    scrollView.delegate = self;
    NSLog(@"%@", scrollView);

    NSLog(@"offset: %.1f", scrollView.contentOffset.y);
    NSLog(@"inset: %.1f", scrollView.contentInset.top);
    [scrollView setContentInset: UIEdgeInsetsMake(44, 0, 0, 0)];
    NSLog(@"offset: %.1f", scrollView.contentOffset.y);
    NSLog(@"inset: %.1f", scrollView.contentInset.top);

    [scrollView setContentOffset: CGPointMake(20, 20) animated: NO];
    NSLog(@"offset: %.1f", scrollView.contentOffset.y);



    scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(20, 240, 200, 200)];
    [self.view addSubview: scrollView];
    scrollView.delegate = self;
    NSLog(@"%@", scrollView);

    NSLog(@"offset: %.1f", scrollView.contentOffset.y);
    NSLog(@"inset: %.1f", scrollView.contentInset.top);
    [scrollView setContentInset: UIEdgeInsetsMake(44, 0, 0, 0)];
    NSLog(@"offset: %.1f", scrollView.contentOffset.y);
    NSLog(@"inset: %.1f", scrollView.contentInset.top);

    [scrollView setContentOffset: CGPointMake(20, 20) animated: NO];
    NSLog(@"offset: %.1f", scrollView.contentOffset.y);
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll");
}

- (UIScrollView *)getScrollViewFromWebView: (UIWebView*) webView {
    UIScrollView *scrollView = nil;

    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:@"5.0" options:NSNumericSearch] != NSOrderedAscending) {
        return webView.scrollView;
    }
    else {
        for (UIView *subview in [webView subviews]) {
            if ([subview isKindOfClass:[UIScrollView class]]) {
                scrollView = (UIScrollView *)subview;
            }
        }

        if (scrollView == nil) {
            NSLog(@"Couldn’t get default scrollview!");
        }
    }
    return scrollView;
}

iOS 4.0的结果日志:

2012-03-14 18:57:17.943 Test[9009:40b] System: iPhone OS, iOS 4.0.2

2012-03-14 18:57:17.970 Test[9009:40b] <UIScrollView: 0x5e12610; frame = (0 0; 200 200); clipsToBounds = YES; autoresize = H; layer = <CALayer: 0x5e127f0>; contentOffset: {0, 0}>
2012-03-14 18:57:17.971 Test[9009:40b] offset: 0.0
2012-03-14 18:57:17.971 Test[9009:40b] inset: 0.0
2012-03-14 18:57:17.972 Test[9009:40b] offset: 0.0
2012-03-14 18:57:17.972 Test[9009:40b] inset: 44.0
2012-03-14 18:57:17.972 Test[9009:40b] scrollViewDidScroll
2012-03-14 18:57:17.973 Test[9009:40b] offset: 20.0

2012-03-14 18:57:17.973 Test[9009:40b] <UIScrollView: 0x5e14c50; frame = (20 240; 200 200); clipsToBounds = YES; layer = <CALayer: 0x5e14240>; contentOffset: {0, 0}>
2012-03-14 18:57:17.974 Test[9009:40b] offset: 0.0
2012-03-14 18:57:17.974 Test[9009:40b] inset: 0.0
2012-03-14 18:57:17.974 Test[9009:40b] scrollViewDidScroll
2012-03-14 18:57:17.975 Test[9009:40b] offset: -44.0
2012-03-14 18:57:17.975 Test[9009:40b] inset: 44.0
2012-03-14 18:57:17.976 Test[9009:40b] scrollViewDidScroll
2012-03-14 18:57:17.976 Test[9009:40b] offset: 20.0
2012-03-14 18:57:18.468 Test[9009:40b] scrollViewDidScroll
2012-03-14 18:57:19.033 Test[9009:40b] scrollViewDidScroll

iOS 5.0的结果日志:

2012-03-14 18:59:08.210 Test[9071:40b] System: iPhone OS, iOS 5.0

2012-03-14 18:59:08.227 Test[9071:40b] <_UIWebViewScrollView: 0x6829020; frame = (0 0; 200 200); clipsToBounds = YES; autoresize = H; layer = <CALayer: 0x6829230>; contentOffset: {0, 0}>
2012-03-14 18:59:08.227 Test[9071:40b] offset: 0.0
2012-03-14 18:59:08.228 Test[9071:40b] inset: 0.0
2012-03-14 18:59:08.228 Test[9071:40b] offset: 0.0
2012-03-14 18:59:08.229 Test[9071:40b] inset: 44.0
2012-03-14 18:59:08.229 Test[9071:40b] scrollViewDidScroll
2012-03-14 18:59:08.230 Test[9071:40b] offset: 20.0

2012-03-14 18:59:08.230 Test[9071:40b] <UIScrollView: 0xca217a0; frame = (20 240; 200 200); clipsToBounds = YES; layer = <CALayer: 0xca0c880>; contentOffset: {0, 0}>
2012-03-14 18:59:08.231 Test[9071:40b] offset: 0.0
2012-03-14 18:59:08.231 Test[9071:40b] inset: 0.0
2012-03-14 18:59:08.232 Test[9071:40b] scrollViewDidScroll
2012-03-14 18:59:08.232 Test[9071:40b] offset: -44.0
2012-03-14 18:59:08.232 Test[9071:40b] inset: 44.0
2012-03-14 18:59:08.233 Test[9071:40b] scrollViewDidScroll
2012-03-14 18:59:08.233 Test[9071:40b] offset: 20.0

【讨论】:

  • 那么你是如何为 UIWebView 的 UIScrollView 设置委托的呢?你能告诉我们一些你的代码sn-p吗?
  • sry,我之前用标准的滚动视图测试过。但 iOS 4 和 5 之间仍然没有区别..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
相关资源
最近更新 更多