iOS - 在Scroll上的UIWebview部分消失

时间:2022-08-24 23:25:57

I've implemented a custom header view that is not part of a UITableView. However, it responds to scrollView delegate methods to adjust the NSLayoutConstraint constant of it's height. The header view contains a web view that renders a static image. The header view moves on and off the screen as expected during tableView scrolling. When scrolling is fast (up/down) it works as expected. However, if you scroll down slowly the content of the UIWebview disappears. iOS  - 在Scroll上的UIWebview部分消失

我已经实现了一个不属于UITableView的自定义标题视图。但是,它响应scrollView委托方法来调整NSLayoutConstraint常量的高度。标题视图包含呈现静态图像的Web视图。在tableView滚动期间,标题视图按预期在屏幕上移动和移出。当滚动快速(向上/向下)时,它按预期工作。但是,如果您慢慢向下滚动,UIWebview的内容将消失。

I had initially thought it may have something to do with the webView's scrollView.contentSize but that seems to be adjusting correctly (based on NSLog). If you take a look at the above gif, the orange is the background of the UIWebview, so only the actual content of the html is vanishing; the view itself is intact. Anyone else run into this issue?

我最初认为它可能与webView的scrollView.contentSize有关,但似乎正在调整(基于NSLog)。如果你看一下上面的gif,橙色是UIWebview的背景,所以只有html的实际内容消失了;视图本身完好无损。其他人遇到这个问题?

Relevant code:

相关代码:

#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);
}

What I've tried:

我尝试过的:

  • forcing the webview to layoutSubviews [self.webView layoutSubViews];
  • 强制webview到layoutSubviews [self.webView layoutSubViews];
  • collecting all subviews of UIWebview into an array and adjust their height as the scrolling happens.
  • 将UIWebview的所有子视图收集到一个数组中,并在滚动发生时调整它们的高度。
  • adjusting the contentView of the webView.scrollView as scrolling happens.
  • 调整webView.scrollView的contentView作为滚动发生。
  • a bunch of other stuff i can't remember. suggest something and I'll tell you if i've tried it.
  • 一堆我不记得的其他东西。提出一些建议,我会告诉你我是否尝试过。

2 个解决方案

#1


1  

OP was able to resolve the issue by using WKWebView instead of UIWebView.

OP能够通过使用WKWebView而不是UIWebView来解决问题。

#2


0  

Can you force the webview to layout when changing the tableView contentOffset?

更改tableView contentOffset时,可以强制webview进行布局吗?

[self.webview setNeedsLayout]

[self.webview setNeedsLayout]

#1


1  

OP was able to resolve the issue by using WKWebView instead of UIWebView.

OP能够通过使用WKWebView而不是UIWebView来解决问题。

#2


0  

Can you force the webview to layout when changing the tableView contentOffset?

更改tableView contentOffset时,可以强制webview进行布局吗?

[self.webview setNeedsLayout]

[self.webview setNeedsLayout]