senty senty - 5 months ago 21
Swift Question

Rearrange autoLayout after changes during view

I have 2 TableViews (one at top, other at bottom) in Storyboard and auto-layout is set and working. For the TableViews, I am retrieving the data from database, thus it won't instantly display the TableView content. After retrieving data is completed, in

numberOfRowsInSection
I am changing the height of the TableView depending on the count.

if tableView == self.TableView1 {
count = tableView1Content.count

if count != nil {
let cellSize = 100

switch count! {
case 0:
tableView.frame.size.height = CGFloat(0)
break;
case 1:
tableView.frame.size.height = CGFloat(cellSize)
case 2:
tableView.frame.size.height = CGFloat(cellSize * 2)
default:
print("D")
break;
}
}
}


So I can change the height of the TableView1. TableView2 has a 'top space to TableView1' of 10, but as the height change happens after the view is loaded, TableView2 doesn't adjust itself to keep the 'top space to TableView1'

Answer

You are changing the frame size not the height constraint constant. You should connect height constraint (IBOutlet) and change the height, after change call self.view.layoutIfNeeded()

Here example of animating constraints.

Comments