2012-03-13 57 views
1

我有以下代碼設置contentInset爲一個UIWebView的滾動視圖UIWebView的滾動視圖中的iOS 4不叫contentOffset

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中,它不......任何想法,爲什麼這是,以及如何解決它?

+0

'scrollViewDidScroll'被調用,當你設置insets(在ios5中)?我不認爲這應該發生..!? – calimarkus 2012-03-13 18:21:34

+0

失調保持在相同的值,不管插入。 (例如0,如果還沒有改變) – calimarkus 2012-03-13 18:27:24

+0

@ jaydee3是的,這是正確的..我刪除了setContentInset,它沒有調用iOS 5上的scrollViewDidScroll ..但是在iOS 4上,它在設置內容插入時從不調用scrollViewDidScroll,這是蘋果的錯誤? – xonegirlz 2012-03-13 18:35:36

回答

2

好的我在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時,它會相應地更正偏移量。

所以這不是一個錯誤。但是應該打電話給代表。在我的測試中 - 確實如此。

OK我的完整測試:我看到4.0和5.0之間唯一的區別是ScrollView是5.0中的_UIWebViewScrollView。此外,webview-scrollview的行爲與標準scrollview的行爲不同。在標準的scrollView中調用GETS,在webviewScrollView中調用GETS。

完整的測試代碼:

- (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 
+0

那麼你是如何設置UIWebView的UIScrollView的委託?你能告訴我們一些你的代碼片段嗎? – xonegirlz 2012-03-14 16:39:56

+0

sry,我以前用標準的scrollview測試過。但iOS 4和5之間仍然沒有區別。 – calimarkus 2012-03-14 18:01:06

相關問題