lundi 23 mars 2015

Suggested pattern for view controller frame setting?

I've run into the following sequence of actions many times in iOS dev, and never quite known what the suggested pattern is for implementation:


1) A view controller A creates a new view controller B


2) A sets the frame of B's view


3) B then creates its own subviews, which need to be sized relative to its own view's frame


Note that this assumes B can't figure out its own frame, it needs to be set from a higher order VC.




So, given the following code:



b = CustomViewController()
b.frame = CGRectMake(...)


one will find that if they try to reference self.frame inside of B's viewDidLoad, the frame is not yet available. So that approach fails. It is possible to add a new method to B like loadViews or something, or even leverage layoutSubviews, but both of those are clumsy or potentially incorrect choices.




Another approach I've tried is making a custom initializer equivalent to initWithFrame, and then putting the new view creation right in the init, and while that works, it seems counter to Apple's basic setup for Cocoa, wherein UIViews have initWithFrame, and UIViewControllers simply have init or initWithNibName.


What is the recommend pattern, here?


Aucun commentaire:

Enregistrer un commentaire