Get height of UITableView without scroll bars

前端 未结 4 1574
野趣味
野趣味 2021-01-31 02:25

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

相关标签:
4条回答
  • 2021-01-31 03:06

    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];
    } 
    
    0 讨论(0)
  • 2021-01-31 03:07

    Obligatory Swift 3.0 & 2.2 answer.

    var tableViewHeight: CGFloat {
        tableView.layoutIfNeeded()
    
        return tableView.contentSize.height
    }
    
    0 讨论(0)
  • 2021-01-31 03:31

    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;
    }
    
    0 讨论(0)
  • 2021-01-31 03:32

    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.

    0 讨论(0)
提交回复
热议问题