好的,我在 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