Get height of UITableView without scroll bars
I need to get the full height of a UITableView
(i.e. the height at which there would be nothing more to scroll). Is there any way to do this?
I've tried [tableView sizeThatFits:CGSizeZero]
, but that only returns 开发者_StackOverflow社区a 0x0 CGSize
.
Try the contentSize
method, which is inherited from UITableView’s superclass, UIScrollView. However, you may find that contentSize
returns an incorrect or out of date value, so you should probably call layoutIfNeeded
first to recalculate the table’s layout.
- (CGFloat)tableViewHeight
{
[tableView layoutIfNeeded];
return [tableView contentSize].height;
}
Obligatory Swift 3.0 & 2.2 answer.
var tableViewHeight: CGFloat {
tableView.layoutIfNeeded()
return tableView.contentSize.height
}
Try passing in a different CGSize parameter instead of CGSizeZero. The sizeThatFits: method uses that parameter to calculate its result. Try passing in self.view.size from whatever class is making that call.
If a table view rows count changed and you indeed need to know the content size of table view incorporating the last changes, I didn't find that layoutIfNeeded method actually helps.
After a little bit hacking, I get to know how to force table view recalculate its content size. In my case, it is enough to reset table view frame to get it working:
- (CGSize)com_lohika_contentSize
{
CGRect theFrame = self.frame;
self.frame = CGRectZero;
self.frame = theFrame;
[self layoutIfNeeded];
return [self contentSize];
}
精彩评论