SlideShare a Scribd company logo
Standford 2015 iOS讀書會
week5
1. View Controller Lifecycle, Autolayout
2. Scroll View and Closure Capture
彼得潘
課程範例download
http://web.stanford.edu/class/cs193p/cgi-bin/drupal/
View Controller
Lifecycle
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
只做⼀一次
auto layout的UI直接改frame無效
此時UI元件的frame還不是auto layout算出的frame
storyboard上只要勾選use auto layout,就算沒設定constraint,
storyboard會依排版位置⾃自動補上auto layout
可能被呼叫多次
在viewDidLoad & viewWillAppear做太多事會卡住UI
viewDidAppear時,UI元件的frame才是auto layout計算後排版的結果
在viewWillDisappear 做太多事會卡住UI
可能被呼叫多次
viewWillDisappear可以把⼀一些UI元件拿掉,節省記憶體
viewDidLayoutSubviews裡UI元件的frame已是auto layout計算的結果
畫⾯面重新排版時,viewWillLayoutSubviews & viewDidLayoutSubviews會被呼叫
auto layout發⽣生在viewWillLayoutSubviews & viewDidLayoutSubviews之間
func setNeedsLayout()
UIView
要求下次更新畫⾯面時重新排版
func layoutIfNeeded()
如果在setNeedsLayout後⾺馬上呼叫layoutIfNeeded,⽴立即更新
func setNeedsDisplay()
重新繪製view
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
rotate
rotate
- (NSUInteger)supportedInterfaceOrientations;
struct UIInterfaceOrientationMask : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)
static var Portrait: UIInterfaceOrientationMask { get }
static var LandscapeLeft: UIInterfaceOrientationMask { get }
static var LandscapeRight: UIInterfaceOrientationMask { get }
static var PortraitUpsideDown: UIInterfaceOrientationMask { get }
static var Landscape: UIInterfaceOrientationMask { get }
static var All: UIInterfaceOrientationMask { get }
static var AllButUpsideDown: UIInterfaceOrientationMask { get }
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
rotate
UINavigationController &
UITabBarController的rotate
接收memory waring: AppDelegate & view controller
cell的awakeFromNib()被呼叫時,
outlet已經設定
xib載⼊入時,當awakeFromNib被呼叫,outlet也已設定
let nib = UINib(nibName: "Red", bundle: nil)
let array = nib.instantiateWithOwner(nil , options: nil )
view controller的
init(coder aDecoder: NSCoder) &
awakeFromNib()被呼叫時,
outlet都還沒設定
Autolayout
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
神奇的Autolayout公式
神奇的Autolayout公式
let imageView = UIImageView(frame: CGRect(x: 10, y: 10, width: 300, height: 300))
self.view.addSubview(imageView)
imageView.image = UIImage(named: "baby.png")
imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
let bottomConstraint = NSLayoutConstraint(item: imageView, attribute:
NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute:
NSLayoutAttribute.Bottom, multiplier: 1, constant: -10)
self.view.addConstraint(bottomConstraint)
visual format language
http://www.knowstack.com/swift-autolayout-visual-format-
language-sample-code/
調整item欄位
調整relation欄位
調整constant & multiplier
設定20%的寬度
constrain to margins
margin
修改constraint
拉outlet到constraint
layout guide
status bar, navigation bar, tab bar, 熱點,電話來電
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
5 *4 和 4*5,但需設定4種
利⽤用anyk其實只要設定2種
ex: 在any height compact width設定的條件也會套⽤用在
regular height compact width 和
compact height compact width
baseline alignment
This lines up the views according to their baseline, which for
text based views is the bottom of text that does not drop down
(like g, p, j, etc). For non-text views it is the same as the
bottom edge.
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
利⽤用preview預覽
preview的size和語⾔言
leading
快速search元件
search裡輸⼊入⽂文字
按住shift可以⼀一次
設定多個條件
讓2個button置中
⽅方法1: 左右各放⼀一個view
⽅方法2: 將兩個button放在⼀一個另⼀一個view,再將此view置中
設定action的argument
{ }() : 執⾏行closure
static let database: Dictionary<String, User> = {
var theDatabase = Dictionary<String, User>()
for user in [
User(name: "John Appleseed", company: "Apple", login: "japple", password: "foo"),
User(name: "Madison Bumgarner", company: "World Champion San Francisco Giants",
login: "madbum", password: "foo"),
User(name: "John Hennessy", company: "Stanford", login: "hennessy", password: "foo"),
User(name: "Bad Guy", company: "Criminals, Inc.", login: "baddie", password: "foo")
] {
theDatabase[user.login] = user
}
return theDatabase
}()
Images.xcassets
依device客製
依size客製
Images.xcassets
UI元件的expend & compress
hugging: content does not want to grow
compression: content does not want to shrink
⼤大家的compression都是750
解法:將image的compression
設成700
demo
197 * 294.5
image <UIImageView: 0x7ff041682930; frame = (16 253.5; 197 294.5); autoresize = RM+BM;
userInteractionEnabled = NO; layer = <CALayer: 0x7ff041682c70>>
demo
image <UIImageView: 0x7fccb9f317a0; frame = (16 142; 0 406); autoresize = RM+BM;
userInteractionEnabled = NO; layer = <CALayer: 0x7fccb9f31ae0>>
demo
設定image view的hugging
設定>=
設定label的trailing space
增加label的horizontal
compression
設定圖⽚片⽐比例
private var image: UIImage? {
get {
return imageView?.image
}
set {
imageView?.image = newValue
if let constrainedView = imageView {
if let newImage = newValue {
aspectRatioConstraint = NSLayoutConstraint(
item: constrainedView,
attribute: .Width,
relatedBy: .Equal,
toItem: constrainedView,
attribute: .Height,
multiplier: newImage.aspectRatio,
constant: 0)
} else {
aspectRatioConstraint = nil
}
}
}
}
demo
設定size regular height的
layout
只在某個layout
出現某個UI元件或constraint
移除某個layout下的所有
constraint
1 2
按X
scroll view
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
demo
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let image = UIImage(named: "book")
imageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 1000, height: 1000))
imageView.image = image
self.myScrollView.addSubview(imageView)
self.myScrollView.contentSize = CGSize(width: 1000, height: 1000)
self.myScrollView.delegate = self
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let rect = self.imageView.convertRect(self.myScrollView.bounds, fromView:
self.myScrollView)
println("rect (rect)")
}
var directionalLockEnabled: Bool
限制某個⽅方向,不能同時⽔水平垂直滑動
self.myScrollView.contentInset = UIEdgeInsets(top: 10, left: 100, bottom: 0, right: 0)
⼀一開始contentOffset的位置是-10, -100
func zoomToRect(rect: CGRect, animated: Bool)
顯⽰示放⼤大的view裡的rect區塊
(viewForZoomingInScrollView回傳的view)
放⼤大時,viewForZoomingInScrollView回傳的view的subview也會⼀一起放⼤大
sizeToFit()
ex: image view縮放到圖⽚片的⼤大⼩小
label縮放到⽂文字的⼤大⼩小
label縮放時,會以⺫⽬目前的寬度為最⼤大寬度,計算需要的⾼高度
let label = UILabel()
label.text = "有⼀一段⾛走過的路我不會忘,有⼀一個愛過的⼈人放在⼼心上,雖然啊⼀一路跌跌撞撞,
也是種成⻑⾧長,我不怕 不是逞強"
self.view.addSubview(label)
label.numberOfLines = 0
label.frame = CGRect(x: 0, y: 30, width: 100, height: 0)
label.sizeToFit()
self.view .addSubview(label)
check view是否在畫⾯面上
if view.window != nil {
fetchImage()
}
以navigation controller為例
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture
weak & unowned
希望它是不變的常數還是可變的變數,若是前者則選
unowned。
weak只能作⽤用於物件
是否可以為nil,若接受nil只能選weak。

More Related Content

PPT
Social engineering and Phishing
PPTX
The social dilemma
PPTX
Short & Brief Life of Steve Jobs
PPTX
Cyber crimes presentation
PPTX
Hacking
PPTX
Elon Musk
PPT
Cyber Crime
PPT
Phishing
Social engineering and Phishing
The social dilemma
Short & Brief Life of Steve Jobs
Cyber crimes presentation
Hacking
Elon Musk
Cyber Crime
Phishing

What's hot (20)

PPTX
Steve Jobs Biography
PPTX
Hacking and Types of Hacker.
PPTX
Elon musk
PDF
Cyber Security
PPTX
Social media privacy issues
PPTX
Leadership style
PPTX
Ethical Hacking & Network Security
PPTX
PPT
Interactive Ppt Project The Brain
PPTX
Elon musk
PPTX
Elon musk
PPT
PPTX
Internet addiction
PPTX
The Theory of Dreaming
PPTX
Who is Elon Musk - a Simple PPT
PPTX
introduction of facebook
PPT
CH 4 Nature Nurture
PPTX
Social Media Privacy
PPTX
Advantages & Disadvantages of Social Networking
PPTX
About Elon Musk IFM.pptx
Steve Jobs Biography
Hacking and Types of Hacker.
Elon musk
Cyber Security
Social media privacy issues
Leadership style
Ethical Hacking & Network Security
Interactive Ppt Project The Brain
Elon musk
Elon musk
Internet addiction
The Theory of Dreaming
Who is Elon Musk - a Simple PPT
introduction of facebook
CH 4 Nature Nurture
Social Media Privacy
Advantages & Disadvantages of Social Networking
About Elon Musk IFM.pptx
Ad

Viewers also liked (17)

PDF
Standford 2015 week3: Objective-C Compatibility, Property List, Views
PDF
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
PDF
Standford 2015 week6
PDF
第一次程式親密接觸
PDF
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
PDF
為愛打造App
PDF
Standford 2015 week9
PDF
Standford 2015 week8
PDF
打造你的第一個iPhone APP
PDF
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
PDF
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
PDF
打造你的第一個 iOS App
PDF
你的程式開發初體驗 (以Swift為例)
PDF
如何變成iOS App開發魔法師
PDF
不能承受的感動 - iOS App實機測試
PDF
利用 iOS App 技術創業的 13 個方法
PPTX
iOS Coding Best Practices
Standford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week6
第一次程式親密接觸
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
為愛打造App
Standford 2015 week9
Standford 2015 week8
打造你的第一個iPhone APP
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
打造你的第一個 iOS App
你的程式開發初體驗 (以Swift為例)
如何變成iOS App開發魔法師
不能承受的感動 - iOS App實機測試
利用 iOS App 技術創業的 13 個方法
iOS Coding Best Practices
Ad

Similar to Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture (20)

ODP
How To Build a Multi-Field Search Page For Your XPages Application
PPTX
Android Oreo
PDF
Engineering the New LinkedIn Profile
KEY
Desenvolvimento iOS - Aula 4
PDF
Ui perfomance
PDF
The Dojo Build System
PPTX
Printing photos-html-using-android
ODP
Desenvolvendo uma aplicação híbrida para Android e IOs utilizando Ionic, aces...
PDF
iOS performance: tips and tricks to do it better
PDF
Responsive websites. Toolbox
KEY
Introduction to Palm's Mojo SDK
PDF
Android Wear: A Developer's Perspective
PDF
MOPCON 2014 - Best software architecture in app development
PDF
Leaving Interface Builder Behind
PDF
Data Binding
PDF
Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...
PDF
Banshun - OSGi-less modularity for Spring
PPTX
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
KEY
Kirin - Making Single Page Web Apps with a Native UI
ODP
IBM Lotusphere 2013 AD109: Using the IBM® Sametime® Proxy SDK: WebSphere Port...
How To Build a Multi-Field Search Page For Your XPages Application
Android Oreo
Engineering the New LinkedIn Profile
Desenvolvimento iOS - Aula 4
Ui perfomance
The Dojo Build System
Printing photos-html-using-android
Desenvolvendo uma aplicação híbrida para Android e IOs utilizando Ionic, aces...
iOS performance: tips and tricks to do it better
Responsive websites. Toolbox
Introduction to Palm's Mojo SDK
Android Wear: A Developer's Perspective
MOPCON 2014 - Best software architecture in app development
Leaving Interface Builder Behind
Data Binding
Android: the Single Activity, Multiple Fragments pattern | One Activity to ru...
Banshun - OSGi-less modularity for Spring
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
Kirin - Making Single Page Web Apps with a Native UI
IBM Lotusphere 2013 AD109: Using the IBM® Sametime® Proxy SDK: WebSphere Port...

More from 彼得潘 Pan (6)

PDF
創作 MusicKit 告白情歌
PDF
如何變成 iOS App 開發魔法師 (1 小時)
PDF
如何變成 iOS App 開發魔法師
PDF
Xcode 的 git 版本管理
PDF
消滅永生不死吸血鬼物件的 ARC
PDF
iOS 7 SDK特訓班
創作 MusicKit 告白情歌
如何變成 iOS App 開發魔法師 (1 小時)
如何變成 iOS App 開發魔法師
Xcode 的 git 版本管理
消滅永生不死吸血鬼物件的 ARC
iOS 7 SDK特訓班

Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View and Closure Capture