2017-03-23 80 views
0

我已經實現了一個不屬於UITableView的自定義標題視圖。但是,它響應scrollView委託方法來調整其高度的NSLayoutConstraint常量。標題視圖包含呈現靜態圖像的Web視圖。在tableView滾動期間,標題視圖按照預期在屏幕上和屏幕之外移動。當滾動速度很快時(向上/向下),它按預期工作。但是,如果向下滾動,UIWebview的內容會消失。 enter image description hereiOS - UIWebview在滾動上部分消失

我最初以爲它可能與webView的scrollView.contentSize有關,但似乎是正確調整(基於NSLog)。如果你看看上面的gif,橙色是UIWebview的背景,所以只有html的實際內容正在消失;視圖本身是完整的。其他人遇到這個問題?

相關代碼:

#define kMaxHeaderHeight 160.0 
#define kMinHeaderHeight 0.0 
-(void)setupWebview 
{ 
    self.webView.scrollView.scrollEnabled = false; 
    self.webView.backgroundColor = [UIColor orangeColor]; 

    NSString *html = 
    @"<head>" 
    @"<style>" 
    @"body {" 
    @"padding: 0px;" 
    @"font-family: Arial;" 
    @"width: 100%;" 
    @"margin: 0px;" 
    @"text-align: center;" 
    @"}" 
    @".image {" 
    @"position: absolute;" 
    @"margin: 0px;" 
    @"bottom: 0;" 
    @"padding: 0px;" 
    @"}" 
    @"</style>" 
    @"</head>" 
    @"<body>" 
    @"<a>" 
    @"<img class='image' src='http://mgk.com/wp-content/uploads/2013/05/bedbug_ICON.png' />" 
    @"</a>" 
    @"</body>"; 

    [self.webView loadHTMLString:html baseURL:nil]; 
} 
-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (scrollView != self.tableView) return; 

    CGFloat scrollDiff = scrollView.contentOffset.y - previousScrollOffset; 
    CGFloat absoluteTop = 0.0; 
    CGFloat absoluteBottom = scrollView.contentSize.height - scrollView.frame.size.height; 
    BOOL isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop; 
    BOOL isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom; 

    CGFloat newHeight = self.headerViewHeightConstraint.constant; 
    if (isScrollingDown) { 
     newHeight = MAX(kMinHeaderHeight, self.headerViewHeightConstraint.constant - fabs(scrollDiff)); 
    } else if (isScrollingUp) { 
     newHeight = MIN(kMaxHeaderHeight, self.headerViewHeightConstraint.constant + fabs(scrollDiff)); 
    } 

    if (newHeight != self.headerViewHeightConstraint.constant) { 
     self.headerViewHeightConstraint.constant = newHeight; 
     [self updateScrollPosition:previousScrollOffset]; 
    } 



    previousScrollOffset = scrollView.contentOffset.y; 
} 

-(void)updateScrollPosition:(CGFloat)postition 
{ 
    self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, postition); 
} 

我已經試過:

  • 迫使web視圖來layoutSubviews [self.webView layoutSubViews]
  • 將UIWebview的所有子視圖收集到一個數組中,並在滾動發生時調整其高度。
  • 發生滾動時調整webView.scrollView的contentView。
  • 一堆我不記得的東西。建議一些事情,我會告訴你,如果我已經嘗試過。

回答

1

OP能夠使用,而不是UIWebView的WKWebView來解決問題。

0

你可以在更改tableView contentOffset時強制webview佈局嗎?

[self.webview setNeedsLayout]

+0

感謝回覆@ Garfield81。我確實嘗試過,沒有奏效。我將添加一個我嘗試過的操作列表;我開始認爲這是一個iOS錯誤。 – Warblr

+0

你可以嘗試WKWebView而不是UIWebView? – Garfield81

+0

偉大的建議,@ Garfield81。我不得不以編程方式添加視圖,並以編程方式設置所有約束,但WKWebview似乎是要走的路。謝謝。如果您想將此作爲「答案」,我會盡快接受。 – Warblr