Adjust the scroll height according to the height of his children table in SWIFT

I have a viewController, with a table view inside a scroll.

I need to programmatically update the height of the tableview, and then the height of the scrollview (the scroll height depends on the height of the table).

-my View Controller -View -ScrollView -Container View -TableView 

So, I wrote this code in viewDidAppear of my viewController:

  override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) var frame:CGRect = self.tableView.frame frame.size.height = tableView.contentSize.height self.tableView.frame = frame scrollView.contentSize=CGSizeMake(scrollView.frame.size.width, tableView.frame.origin.y + tableView.frame.height) } 

If I check the console, viewDidAppear is called BEFORE all cellForRowAtIndexPath. In this case, everything works the way I want.

In my Controller view, I have a button that opens the modalViewController. In this modalViewController, I add new data to my table view (like a new cell).

When I close my modalViewController using shutViewControllerAnimated, viewDidAppear is called, but AFTER all cellForRowAtIndexPath. My viewController does not display correctly, the image of the table is cropped, I can not scroll it at the bottom of the table. My code is not working.

I work with Swift and auto-layout.

+5
source share
1 answer

Try calling this in a view controller:

 automaticallyAdjustsScrollViewInsets = false 

You can also install it in Interface Builder if you prefer

0
source

Source: https://habr.com/ru/post/1210229/


All Articles