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

Post on 28-Jul-2015

566 views 6 download

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

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

只做⼀一次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

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

神奇的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, 熱點,電話來電

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.

利⽤用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 growcompression: 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

demooverride 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為例

weak & unowned希望它是不變的常數還是可變的變數,若是前者則選unowned。

weak只能作⽤用於物件

是否可以為nil,若接受nil只能選weak。