Share
Embed
Auto Layout with an extended Visual Format Language
AltConf19 on Thursday 11:00-11:10 (LT) at San Jose Marriott
http://altconf.com
Video Archive: https://www.youtube.com/watch?v=3n3AeUuWavw
Transcript
39 slides
-
Copyright © ZOZO Technologies, Inc. All Rights Reserved. ;0;05FDIOPMPHJFT *OD
#"/+VO!CBOKVO "VUP-BZPVUXJUIBOFYUFOEFE 7JTVBM'PSNBU-BOHVBHF "MU$POG -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 4QFBLFS1SPGJMF ;0;05FDIOPMPHJFT
*OD %FWFMPQNFOU%JWJTJPO #"/+VO!CBOKVO -PWF-JWFS NBD04 $SFBUF-JWF1IPUPTGSPN.PWJFT 'MJDL4,, J04 +BQBOFTF$VTUPN,FZCPBSE&YUFOTJPO 4JEF1SPKFDUT -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. w 5IFMBSHFTUGBTIJPOPOMJOFTIPQQJOHXFCTJUFJO+BQBO
w 0WFS TUPSFTP⒎FSJOHNPSFUIBO CSBOET "TPG%FDTU w "UBOZHJWFOUJNF NPSFUIBO JUFNTBSFBWBJMBCMFGPSQVSDIBTF JOBEEJUJPOPGNPSFUIBO OFXJUFNT BWFSBHF QFSEBZ w 4BNFEBZEFMJWFSZTFSWJDFTBSFBWBJMBCMFJOMJNJUFEBSFBTXJUIJO+BQBO IUUQT[P[PKQ -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. "VUP-BZPVUXJUI$PEF 7JTVBM
'PSNBU -BOHVBHF -BZPVU "ODIPST %4- -JCSBSZ -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. "VUP-BZPVUXJUI7'- Pros
Multiple constraints at once, using a compact expression ASCII-art like, readable diff Create only valid constraints "H:|-[icon(==32)]-[label]-(>=8)—|" -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. "VUP-BZPVUXJUI7'- Cons
VFL itself is not validated at compile time Needs some boilerplates to use No explicit support for neither Layout Margins or Safe Area -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. override func
loadView() { super.loadView() let views = [ "icon": iconView, "name": nameLabel] views.values.forEach { view.addSubview($0) $0.translatesAutoresizingMaskIntoConstraints = false } let metrics = ["p": 8] view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-p-[icon(==64)]", options: [], metrics: metrics, views: views)) view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: iconView nameLabel 1. setup a view hierarchy for views 2. enable auto-layout for views Copyright © ZOZO Technologies, Inc. All Rights Reserved. "VUP-BZPVUXJUIPVU/PSUI-BZPVU -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. override func
loadView() { super.loadView() let views = [ "icon": iconView, "name": nameLabel] views.values.forEach { view.addSubview($0) $0.translatesAutoresizingMaskIntoConstraints = false } let metrics = ["p": 8] view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-p-[icon(==64)]", options: [], metrics: metrics, views: views)) view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-p-[name]-p-|", options: [], metrics: metrics, views: views)) view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[icon(==64)]-p-[name]", options: [], metrics: metrics, views: views)) } 3. write VFL with many parameters -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. /PSUI-BZPVU6TBHF 1.
declare views 2. write VFLs autolayout("…") iconView nameLabel override func loadView() { super.loadView() let autolayout = northLayoutFormat(["p": 8], [ "icon": iconView, "name": nameLabel]) autolayout("H:|-p-[icon(==64)]") autolayout("H:|-p-[name]-p—|") autolayout("V:|-p—[icon(==64)]-p-[name]") } -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 7'-4BGF"SFB VFL
does not support Safe Area But I want to write constraints as easy as VFL -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. &YUFOEJOH7'- Idea
VFL (Apple) Extended VFL (EVFL) autolayout("H:|[header]|") autolayout("H:||[icon]… single vertical bar | = superview bounds double vertical bars || = layout margin bounds -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. &YUFOEJOH7'- Idea
VFL (Apple) Extended VFL (EVFL) autolayout("H:|[header]|") autolayout("H:||[icon]… single vertical bar | double vertical bars || -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 8IZ-BZPVU.BSHJOT Layout
Margins follow Safe Area Layout Margins are active on all devices not only devices with home indicator test layout constraints in any devices -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. -BZPVU.BSHJOT4BGF"SFB Example
Constraining to Layout Margins No constraints ref to Safe Area Layout Margins reflects Safe Area Copyright © ZOZO Technologies, Inc. All Rights Reserved. -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. *NQMFNFOUJOH&7'- VFL
"H:|[icon]… Auto Layout -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. *NQMFNFOUJOH&7'- EVFL
"H:||[icon]… VFL "H:|[icon]… Auto Layout ? -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. *NQMFNFOUJOH&7'- EVFL
Parser AST "H:||[icon]… struct VFL Edge Constraints "H:[icon]… Auto Layout Decomposer + -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 1BSTJOH&7'- EVFL
Parser AST "H:||[icon]… struct parser combinator (kareman/FootlessParser) -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. &7'-"CTUSBDU4ZOUBY5SFF struct
VFL { let orientation: Orientation let firstBound: (Bound, Connection)? let firstView: View let views: [(Connection, View)] var lastView: View {return views.last?.1 ?? firstView} let lastBound: (Connection, Bound)? -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. &7'-"CTUSBDU4ZOUBY5SFF enum
Orientation {case h, v} struct View { let name: String let predicateListWithParens: [Predicate] } enum Bound: String { case superview = "|" case layoutMargin = "||" } -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. &7'-"CTUSBDU4ZOUBY5SFF enum
Orientation {case h, v} struct View { let name: String let predicateListWithParens: [Predicate] } enum Bound: String { case superview = "|" case layoutMargin = "||" } -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 1BSTJOH&7'- static
var parser: Parser<Character, VFL> { let metricName = identifier let positiveNumber: Parser<Character, CGFloat> = numberPa let simplePredicate: Parser<Character, VFL.SimplePredicat let predicateListWithParens: Parser<Character, [VFL.Predi <^> (char("(") *> ({[$0]} <^> VFL.Predicate.parser)) <*> zeroOrMore(char(",") *> VFL.Predicate.parser) <* let predicateList: Parser<Character, VFL.PredicateList> = <|> {.predicateListWithParens($0)} <^> predicateListW let bound: Parser<Character, VFL.Bound> = {_ in VFL.Bound <|> {_ in .superview} <^> string(VFL.Bound.superview. let connection = (VFL.Connection.init) <^> (char("-") *> <|> {_ in VFL.PredicateList.simplePredicate(.positive -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. 1BSTJOH&7'- <|>
{_ in VFL.PredicateList.simplePredicate(.positiveN <|> {_ in VFL.PredicateList.simplePredicate(.positiveN let view = curry(VFL.View.init) <^> (char("[") *> identifier) <*> optional(predicateListWithParens, otherwise: []) < let views = zeroOrMore({a in {(a, $0)}} <^> connection <*> let orientation: Parser<Character, VFL.Orientation> = {_ i <|> {_ in .h} <^> (string("H:") <|> string("")) return curry(VFL.init) <^> orientation <*> optional(tuple <^> bound <*> connection) <*> view <*> views <*> optional(tuple <^> connection <*> bound) } -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. .JOJNVN%JGGGPS4BGF"SFB4VQQPSU double
vertical bars || = stick to layout margin single vertical bar |-p- = stick to superview with a space p -
Copyright © ZOZO Technologies, Inc. All Rights Reserved. .JOJNVN%JGGGPS4BGF"SFB4VQQPSU double
vertical bars || = stick to layout margin