Resize UITableView Height with Auto Layout

Since the introduction of Auto Layout in Xcode, resizing UITableViews or UIViews has changed from updating the frame to updating the constraint. With Auto Layout turned off, the way you would resize a UITableView would be to adjust its frame.

CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = 200;
self.tableView.frame = tableFrame;

With Auto Layout turned on, you have to add a Height constraint and adjust the size of the constraint to resize the UITableView. You can add a Height constraint in your storyboard like so:

I prefer to set the constraint to the largest possible value and then select Less Than or Equal to allow it to shrink appropriately.

Next add an IBOutlet and connect it to the height constraint you just created via storyboards.

@property (weak,nonatomic) IBOutlet NSLayoutConstraint *tableHeightConstraint;

There are several ways to connect your IBOutlet. If you are unfamiliar with how to do this I suggest you read this post.

Now to resize your UITableView, modify your constraint and call needsUpdateConstraints.

self.tableHeightConstraint.constant = 200;
[self.tableView needsUpdateConstraints];

Adjusting Other Constraints
You will probably have to modify other constraints to make this work. I modified a trailing space constraint that would lock it to the bottom of the screen and gave it a lower priority so that my height constraint would take precedence. Adjust your other constraints as needed.

Trailing Space

5 thoughts on “Resize UITableView Height with Auto Layout

  1. I am making a project and have applied some width constraints to a label inside a tableviewcell. However, when I rotate from landscape to portrait my label does not resize. Any suggestions?

    How can I make sure that the width is about the same height as other sc

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Do the Math