I have searched around a lot, and dug through a couple of text books, but what I would really appreciate is a simple explanation of best practice to define UIView subclasses in an iOS app.
I don't need super specific instructions - I am more looking for a quick reference guide that explains what kind of code is meant to live in each of the methods available.
My practice is to:
In my custom UIView subclass I:
if ( !CGRectEqualToRect(__subview.frame, rect) ) { __subview.frame = rect; }
That is what I do in all UIViewControllers. I don't use IB, everything is created programmatically.
Hope it helps!
If using a xib, where can I add/tweak the controls at the start of runtime?
In viewDidLoad
of the view controller.
If building programmatically, should I do this in the ViewController (loadView?) or a separate UIView subclass? If the latter, how do I specify it's file's owener so that if it is added as a subview it knows who its controller is?
Again, I always do this in viewDidLoad
of my view controller, as well.
What do you place in awakeFromNib, loadView, viewDidLoad, UIView.init, UIViewController.init, etc?
Of these, I only worry about viewDidLoad
.
What other methods do you frequently use?
Make sure you use auto resizing masks for your manually created controls to make sure that they handle user interface orientations properly.
Never assume (as many do), that your screen is 320pt wide. Always refer to self.view.size
where you need the size of the current view.
If your controls can't handle user interface orientations changes via auto resizing masks, make sure to use the iOS 5's viewWillLayoutSubviews
to adjust them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With