Custom Layout does work on subviews.

Hi,


Could someone share any insight they might have on this? I have Auto Layout and Size Classes enabled in my Story Board but I do not have any constaints defined. Instead I am using the Story Board dimensions as reference to then proportionally set the frames according to the device screen size.


In viewDidLayoutSubviews, I call a function that iterates through all the SubViews of the Main View of the View Controller and they set the frames of each accordingly. This has worked wonders so far, until I attached a label to one of the subviews. Even if modify the frame of the label, it somehow gets reset to the Story Board coordinates somewhere does the road.


So, the hierarchy is like this:


View Controller -> View -> View (sizes correctly in viewDidLayoutSubviews)

View Controller -> View -> ImageView (sizes correctly in viewDidLayoutSubviews)

View Controller -> View -> View-> Label (frame gets reset to story board coordinates after being set in viewDidLayoutSubviews)


If anyone can offer some help, it would be much appreciated!


Thanks,

Fardin.

Answered by Fardin.Elias in 46713022

Thanks Justin,


I tried to disable Auto Layout, but that also requires the disabling of Size Classes, which resized the storyboard to an iPhone or iPad. I am yet to find out exactly how much I need to change to make so that everything still works as before.


Thanks for the lead! 🙂


Fardin.

Hi Fardin


If AutoLayout is enabled in your storyboard, interface builder is going to fill in missing constraints at build time. This means that any view for which you have not specified any constraints is going to receive a set of four default constraints which position and size the view as it appears in the storyboard. You can see this yourself by setting a breakpoint in -viewDidLayoutSubviews and entering `po [self.view constraints]` in the debugger prompt.


If you plan to handle layout manually, you should disable AutoLayout in your storyboard.

Accepted Answer

Thanks Justin,


I tried to disable Auto Layout, but that also requires the disabling of Size Classes, which resized the storyboard to an iPhone or iPad. I am yet to find out exactly how much I need to change to make so that everything still works as before.


Thanks for the lead! 🙂


Fardin.

I'm not sure how useful size-classes are without Auto Layout.


You can keep AutoLayout enabled and add dummy constraints to all of your views. If you select the Remove At Build Time option for each of these contraints, they will not be compiled into the soryboard and Interface Builder will not add its own default contraints.

My dilemma is that if I disable both Auto Layout and Size Classes, the size of the Storyboard changes which totally throws off every calculation I made to manually position the views and widgets. I have to investigate the extent of the changes to make sure everything still works.


But I have also come up with a solution to still have the current Auto Layout/Size Classes, and manually position the views and subviews. I am keeping a flat hierarchy and using tags to group views together under a virtual hierarchy of sort. It's not ideal, but it does what I need to do at the moment.


I will come back to revisit this issue by disabling Auto Layout and Size Classes later on, and see what happens.


Thanks a lot for your help once again!

Fardin.

Custom Layout does work on subviews.
 
 
Q