Upload
jcgohlke
View
345
Download
3
Embed Size (px)
Citation preview
1024 x 768
View controller lifecycle @ University iOS Meetup
December 2, 2014
9 slides
=Overpass
bebas neue =
image
When will then be now?
Ben Gohlke December 2, 2014 university ios
View Controller Lifecycle
View Lifecycle
• UIViewController loads its view programmatically or via storyboard/nib.
• Several methods are called on UIViewController instance to provide an opportunity for customization.
• Layout
• Content
December 2, 2014 university ios
Programmatic Initialization
• Override loadView method in your view controller.
• Do NOT call super on loadView
• Create a root view object and set its frame (usu. same as window or container)
• Create any subviews and add them to their parent using addSubview:
• If using autolayout, add constraints to subviews
• If not, use viewWillLayoutSubviews and viewDidLayoutSubviews
December 2, 2014 university ios
Load from a nib or storyboard
• Lay out subviews in nib or storyboard
• Xcode packages these objects in an archive which is unpacked at runtime. Your app will gain control of fully instantiated and configured objects.
• Not necessarily laid out though.
December 2, 2014 university ios
@interface MyViewController()
@property (nonatomic) IBOutlet UIButton *myButton; @property (nonatomic) IBOutlet UIButton *myTextField; - (IBAction)myAction:(id)sender;
@end
Tweaking the layout
• viewWillLayoutSubviews, called right before subviews are laid out. Opportunity to override and make changes to view before layout.
• viewDidLayoutSubviews, called right after subviews are laid out (not guaranteed that all subviews have adjusted their layouts).
December 2, 2014 university ios
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) self.view.superview.bounds = CGRectMake(0, 0, 540, 400); }
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { CGFloat imageViewWidth = CGRectGetWidth(self.anImageView.bounds); CGFloat multiplier = imageViewWidth / self.anImageView.image.size.width; CGFloat adjustedHeight = ceilf(self.anImageView.image.size.height * multiplier);
[self.anImageViewHeightConstraint setConstraint:adjustedHeight]; } }
Demo
December 2, 2014 university ios
https://github.com/jcgohlke/University-iOS-Demo-December
@AkhenatenQuestions?
December 2, 2014 university ios