Dynamically resizing a UITableView in a UIViewController created in Storyboard

In the UIViewController on the storyboard, I have a UITableView whose size is specifically defined to have two rows in one section without a header or footer, i.e. height 88.0f . There are some cases where I want to add a third line. So, in viewWillAppear:animated: (and in other logical places), I set the logical pixels above for the 44.0f frame:

 CGRect f = self.tableView.frame; self.tableView.frame = CGRectMake(f.origin.x, f.origin.y, f.size.width, f.size.height + 44.0f); NSLog(@"%@",NSStringFromCGRect(self.tableView.frame)); 

Nothing controversial; pretty standard resizing code, and yet ... That doesn't work! The height of the tableView does not change visually. The NSLog statement reports the expected height ( 132.0f ). Is it because I use storyboards? I am not sure why this is not working.

+6
source share
3 answers

Set an automatic layout limit for the height of the table view in your storyboard. Then plug the restriction into an outlet in the view controller so that you can access the restriction in your code. Set the limit to 88. If you want to change the height of the table view, just change the constant constant to 132.

+14
source

You can change the frame only after the layoutSubviews call is layoutSubviews , which occurs after viewWillAppear . After calling layoutSubviews in UIVIew you can resize.

As Gavin says, if you enable autostart, you can add restrictions to the UITableView through the storyboard, enable the height limit, and change its value as follows:

 constraint.constant = 132.0f 

Otherwise, if you turned off autostart, you can simply change the frame to update the height, but by placing the code in another method, for example viewDidLoad:

0
source

Recently, I try to do what you do. And I have the same problem, the height of the tableview will not change. Now I got the solution, you need to call layoutSubviews after changing the frame. And it works for me.

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height + 44.); [tableView layoutSubviews]; } 

do not put it in viewDidLoad or viewWillAppear: because even layoutSubviews , the frame will not change. put it on viewDidAppear:

0
source

All Articles