Azhar
Azhar

Reputation: 20680

iOS : How to allign UITableView to bottom?

I am trying to create message screen where I put a UITableView in a view with a UITextField and UIButton in its footer.

Problem starts when there is no data or less data for UITableView and its footer reaches to the Top, which should not be, like in below pic.

enter image description here

It suppose to be in the bottom or I could align my UITableView to the bottom as if it has less data atleast footer (which has UITextField and UIButton) should be in the bottom, like in the below pic

enter image description here

I controls hierarchy is like this

enter image description here

Please help, to keep TextFiels and button in the bottom either It has data or NOT.

Upvotes: 0

Views: 945

Answers (2)

DBD
DBD

Reputation: 23233

Under most circumstances, your shouldn't need to put a UITableView inside a UIScrollView since UITableView is already a sub-class of a UIScrollView.

As for the question you asked. Do you really want the text field and button in the table? When you consider most texting apps you want the text field to always be visible, regardless of scrolling up and down through the messages. As such you might want to take it out of the table and put it on the bottom of the screen below your table. Which would incidentally also solve your problem.

Maybe you REALLY want it as a footer. You can still do it. There are 2 ways to accomplish the task.

  1. Check the combined height of all your rows + height of your footer. If this is smaller than your screen space, increase the height of your footer to fill the rest of the space. You'll have to make sure the text field/button aligned to the bottom of your footer.

    // where 'footer' is the view of your footer
    CGFloat newFooterHeight;
    if (sumRowHeight + defaultFooterHeight < view.frame.size.height) {
        newFooterHeight = view.frame.size.height - sunRowHeight;
    } else {
        newFooterHeight = defaultFooterHeight;
    }
    
    CGRect alteredFooterFrame = footer.frame;
    footer.frame.size.height = newFooterHeight;
    footer.frame = alteredFooterFrame;
    
  2. Do almost the identical thing as above, but instead of buffering the size of the footer, you increase the height of the last row.

Upvotes: 3

combinatorial
combinatorial

Reputation: 9561

Don't include the text view and button in the table. Put them in the root view as a sibling of the table. That way you can position it as always at the bottom of the screen.

Upvotes: 0

Related Questions