【发布时间】:2015-02-05 19:42:24
【问题描述】:
我有两个视图,一个 UIWebView 和一个 UITableView,我正在以编程方式扩展它们的高度。这两个视图都位于 UIScrollView 内。 WebView 和 TableView 都禁用了滚动,并且 ScrollView 及其父视图关闭了 Autoresize Subviews。我要做的是将两个项目的高度扩展到适当的大小,然后将 ScrollView 的内容大小扩展到两个项目的总和。现在这行得通。但是,一旦我滚动 TableView 和 WebView 重置到它们的原始高度,但 ScrollView 保持其新的内容大小。
我正在使用以下代码更改 webViewDidFinishLoad 中的高度。
// Set the webview height //
webView.frame = CGRectMake(
webView.frame.origin.x
, webView.frame.origin.y
, webView.frame.size.width
, webView.scrollView.contentSize.height);
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[webView addConstraint:[NSLayoutConstraint
constraintWithItem:webView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:webView.scrollView.contentSize.height]];
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
// Set the table height and location //
self.myTableView.frame = CGRectMake(self.myTableView.frame.origin.x
, webView.frame.origin.y + webView.scrollView.contentSize.height + 8
, webView.frame.size.width
, [tableRows count] * 40);
[self.myTableView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.myTableView addConstraint:[NSLayoutConstraint
constraintWithItem:self.myTableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:[tableRows count] * 40]];
// Update the scrollview //
CGSize newSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, webView.frame.size.height + self.myTableView.frame.size.height);
[super updateViewConstraints];
[self.myScrollView setContentSize:newSize];
我猜我在某处遗漏了一步。
【问题讨论】:
标签: ios objective-c uiscrollview