我已经实现了一个不属于UITableView的自定义标题视图。但是,它响应scrollView委托方法来调整其高度的NSLayoutConstraint常量。标题视图包含呈现静态图像的Web视图。在tableView滚动期间,标题视图按照预期在屏幕上和屏幕之外移动。当滚动速度很快时(向上/向下),它按预期工作。但是,如果向下滚动,UIWebview的内容会消失。 iOS - 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。
- 一堆我不记得的东西。建议一些事情,我会告诉你,如果我已经尝试过。
感谢回复@ Garfield81。我确实尝试过,没有奏效。我将添加一个我尝试过的操作列表;我开始认为这是一个iOS错误。 – Warblr
你可以尝试WKWebView而不是UIWebView? – Garfield81
伟大的建议,@ Garfield81。我不得不以编程方式添加视图,并以编程方式设置所有约束,但WKWebview似乎是要走的路。谢谢。如果您想将此作为“答案”,我会尽快接受。 – Warblr