Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when autoresizing changes the view's bounds

I have a custom UITableViewController that detects when it's view's bounds change and uses that to update the number of thumbnails displayed per tableviewcell. However, when I embed the tableView inside of another view the bounds do not change, although I can confirm visually that it resizes.

I use KVO to detect when the bounds of the view changes. From inside my custom TableViewController's viewDidLoad method:

[self.view addObserver:self forKeyPath:@"frame" options:0 context:nil];

If I make my tableViewController the root view controller for the app and rotate the iPad the bounds are updated with each rotation, and all works perfectly.

However, with my tableview embedded in the nib of another view controller the bounds never gets updated - nor does the frame. Despite that, I can see that the tableview itself is resizing correctly as it rotates into/out of landscape mode - so somewhere in the app the real frame/bounds are changing, and just not being reported. Any idea how I can access what's really happening?

like image 922
user441669 Avatar asked Dec 16 '22 17:12

user441669


2 Answers

A better solution than KVO would be to implement viewDidLayoutSubviews in your view controller. This is called on all bounds changes, once the layout is complete.

like image 95
jrturton Avatar answered Jan 04 '23 17:01

jrturton


It turned out that in the interface builder file the tableview had lost it's referencing outlet as the tableViewController's view. Adding this back solved the problem!

like image 37
user441669 Avatar answered Jan 04 '23 16:01

user441669