ViewController Life Cycle
- awakeFromNib
- prepare for segue
- outlet etting
- ViewDidLoad
- appear and disappear
- Geometry changes
- Low-memory situations
viewDidLoad
- 初始化视图代码
- update UI from Model
注意:geometry is not yet set
viewWillAppear
- 网络任务不要在viewDidLoad而是viewWillAppear
- geometry is set
viewDidAppear
viewWillDisappear
- do some cleanup & “remember what’s going on”
- Do not do anything time-consuming
viewDidDisappear
Geometry change 99% handled with AutoLayout
- viewWillLayoutSubviews()
- viewDidLayoutSubviews()
- setNeedsLayout()
Autorotation
- viewWillTransitionToSize(size: CGSize, withTransitionCoordinator: UIViewControllerTransitionCoordinator)
didReceiveMomoryWarning
awakeFromNib
- sent to all objects that come out of a storyboard
- Happens before outlets are set(before MVC coded)
- Put code somewhere else if at all possible
- Great to set delegate
AutoLayout & Size Classes
设备 | Portrait | Landscape |
---|---|---|
iPhone6+ | 宽:C 长:R | 宽:R 长:C |
iPhone non-6+ | 宽:C 长:R | 宽:C 长:C |
iPad | 宽:R 长:R | 宽:R 长:R |
iPad Split-master | 宽:C 长:R | 宽:C 长:R |
- 获取SizeClasses的方式
let mySizeClasses:UIUserInterfaceSizeClasses = self.traitCollection.horizontalSizeClass
- UIUserInterfaceSizeClasses
- .Compact
- .Regular
- .Unspecified
Autolayout主要在Storyboard中设置,如果要使用代码
NSLayoutConstraint(item:Anyobject, attribute:NSLayoutAttribute, relatedBy:NSLayoutRelation, toItem:Anyobject?, attribute:NSLayoutAttribute, multiplier:CGFloat, constant:CGFloat)
注意:Storyboard中AutoLayout非常重要的的两个概念
Compress压缩,数值越大代表越不希望被压缩
Hug代表有多不希望扩张,如按钮hugging priority高,textField在horizontal中希望扩张,在verital不希望扩张