开发者

How can I get a UITableView tableFooterView to expand to fill the whole parent view?

I have a UIView that I am setting as the property of a UITableView tableFooterView property. In cases where the table view and the footer do not fill the whole parent view, is there a way to determine how much taller I need to make the footer so that it does fill in the remaining space?

My ultimate goal here is to get a delete button to be aligned to the bottom of the view. If the table view is larger than the parent view, I am not going to do anything and the delete button will be o开发者_JAVA技巧ff the view to start which is fine.

EDIT This needs to work on iPad in the form sheet modal type, where the view bounds should only be that of the form sheet, not the whole screen.


Off the top of my head: since UITableViews are essentially UIScrollViews, try using the contentSize.height value of your table view to see how much it occupies the screen. Then, adjust the tableFooterView frame to fill the difference in height from its superview's frame height. Essentially:

CGRect tvFrame = tableView.frame;
CGFloat height = tvFrame.size.height - tableView.contentSize.height;
if (height > MIN_HEIGHT) { // MIN_HEIGHT is your minimum tableViewFooter height
    CGRect frame = tableFooterView.frame;
    tableFooterView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);
}


@octy's answer will work for iOS 9. However, for iOS 10 it seems that the tableView's contentSize includes the tableViewFooter height. In iOS 10 i had to do something like the following:

var rowDataBounds: CGRect {
    get {
        if numberOfSections <= 0 {
            return CGRect(x: 0, y: 0, width: frame.width, height: 0)
        }
        else {
            let minRect = rect(forSection: 0)
            let maxRect = rect(forSection: numberOfSections-1)
            return maxRect.union(minRect)
        }
    }
}

fileprivate func resizeFooterView(){

    if let footerView = tableFooterView {

        var newHeight: CGFloat = 0
        let tvFrame = self.frame;

        if #available(iOS 10, *) {

            newHeight = tvFrame.size.height - rowDataBounds.height - self.contentInset.bottom - self.contentInset.top

        }
        else {

            newHeight = tvFrame.size.height - self.contentSize.height

        }
        if newHeight < 0 {
            newHeight = 0
        }
        let frame = footerView.frame
        if newHeight != frame.height {
            footerView.frame = CGRect(x:frame.origin.x, y:frame.origin.y, width:frame.size.width, height: newHeight)
        }
    }
}

override func layoutSubviews() {
    super.layoutSubviews()
    resizeFooterView()
}


  CGRect parentFrame = myParentView.frame; //tells you the parents rectangle.
  CGRect tableFrame  = myTableView.frame; // tells you the tableView's frame relative to the parent.

  float delta = parentFrame.size.height - tableFrame.size.height - tableFrame.origin.y;

delta is the distance between the bottom of the table and the bottom of it's container view.


Just one idea: if the table view has fixed row height, you could multiply the number of rows by the row height plus some fixed amount. If the row height is not fixed, you could sum up all the various heights.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜