在自动布局设计中,我选择通用尺寸(600x600),并将UITableView拖入故事板中的viewController,此tableView占据整个水平空间,并具有550高度。 我想在tableView中添加一个UIWebView(加载本地静态html文件。)作为页脚,但是在添加之后,webView无法正确显示(就像webView有更多的水平空间)。
我的代码在这里:
- (void)viewDidLoad {
[super viewDidLoad];
[self loadHtml];
}
- (void) loadHtml {
float height = 500; //maybe more , up to content of html
_webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , height)];
...
// load html fille content as html
[ _webview loadHTMLString:html baseURL:nil];
[self.table setTableFooterView: _webview];
}
我调试了_webview的大小,发现它的宽度总是600,即使我在init时更改了它的值。我是怎么做的 当使用自动布局时,这意味着我们不应该使用固定值,但是如何初始化UIView?
注意这是我的错,我没有为tableView添加约束。
答案 0 :(得分:0)
您需要在代码中执行此操作。加载表视图时,创建Web视图并设置框架(只有与表视图相同的宽度才真正重要)。然后,加载Web视图内容并等待代理被告知它已完成。现在,您可以获取Web视图的内容大小滚动视图并更新框架,然后添加为页脚视图。
答案 1 :(得分:0)
如果我理解正确,你想将UIWebView添加为UITableView的页脚。 为了更改UITableView的页脚的高度大小,您可能想要覆盖委托方法:
objc
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section
除此之外,如果您想根据webView的内容更改视图大小,您还要尝试
objc
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
JSKOriginalWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , height)];
[webview loadHTMLString:html baseURL:nil];
return [webview heightForContent];
}
JSKOriginalWebView是原始webview,而heightForContent返回内容的高度。通过这种方式,您可以与自动布局集成。
答案 2 :(得分:0)
只需将tableFooterView的frame属性设置为添加WebView即可。