SWIFT UITableview 根据内容设置自动高度 - XCode - iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33154875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SWIFT UITableview set autoheight according to contents - XCode - iOS
提问by Zahid Gill
Automatically adjust the height of UITableView
according to Contents Dynamically.
I tried the following solution but didn't work for me:
UITableView
根据内容动态自动调整高度。我尝试了以下解决方案,但对我不起作用:
dispatch_async(dispatch_get_main_queue()) {
//This code will run in the main thread:
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
}
回答by Haseeb
I was having the same issue. i have solved it by simply passing the UITableView
content height to UITabelView
frame Height.
我遇到了同样的问题。我已经通过简单地将UITableView
内容高度传递给UITabelView
框架高度来解决它。
func UITableView_Auto_Height()
{
if(self.UITableView.contentSize.height < self.UITableView.frame.height){
var frame: CGRect = self.UITableView.frame;
frame.size.height = self.UITableView.contentSize.height;
self.UITableView.frame = frame;
}
}
Call the above function in viewDidAppear
function of your viewController
.
在viewDidAppear
您的viewController
.
override func viewDidAppear(animated: Bool) {
UITableView_Auto_Height();
}
回答by mackerel
BAD::if(self.UITableView.contentSize.height < self.UITableView.frame.height){
GOOD::if(self.UITableView.contentSize.height > self.UITableView.frame.height){