Professional Documents
Culture Documents
Auto Layout
Silicon Valley iOS Developers' Meetup
March 2015
Tyler Fox
@smileyborg
Layout
What is Layout?
y
x
View height
width
view.frame = CGRect(x: x,
y: y,
width: width,
height: height)
view.frame
Regular
Regular
iPad iPad
Regular Regular
Portrait Landscape
Compact
Compact
iPhone iPhone
Regular Compact
Portrait Landscape
Compact
Regular
iPhone iPhone
Regular 6 Plus Compact 6 Plus
Portrait Landscape
UITraitCollection
horizontalSizeClass Compact
verticalSizeClass Regular
UIWindow
ChildVC1 UIWindow
UIScreen
ParentVC ChildVC2
ParentView
ChildView1 ChildView2
UIViewController
UIView
UIWindow
UIWindow
ParentVC
UIViewController
ChildVC1 ChildVC2
ParentView
UIView
ChildView1 ChildView2
1024 Regular
736 Regular
Dep
-[willRotateToInterfaceOrientation:duration:]
reca
-[didRotateFromInterfaceOrientation:]
self.interfaceOrientation
ted
-[willAnimateRotationToInterfaceOrientation:duration:]
},
completion: { (context) -> Void in
})
}
@end
GitHub Gist
bit.ly/1BHOYoF
✅ What is my size?
UIView.autoSetPriority(750) {
view1.autoPinEdge(.Left,
toEdge: .Right,
ofView: view2,
withOffset: 10.0,
relation: .GreaterThanOrEqual)
}
github.com/smileyborg/PureLayout
✅
func abs(x: Int) -> Int {
return (x < 0) ? -x : x
}
🚫
func purchaseItem(item: Item) {
charge(creditCard, forAmount: item.price)
}
✅
func emptyCart() {
shoppingCart.removeAllItems()
}
✅ Efficient
Changing the constant of installed constraints
⚠️ Less efficient
Removing existing constraints
and installing new constraints
NSAutoresizingMaskLayoutConstraint
Constraints generated for views not using auto layout
NSIBPrototypingLayoutConstraint
Constraints generated by Interface Builder to fix layout ambiguity
view2.autoPinEdge(.Top,
toEdge: .Bottom,
ofView: view1).autoIdentify("view2.top = view1.bottom")
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis;
- (BOOL)hasAmbiguousLayout;
- (void)exerciseAmbiguityInLayout;
@end
Constraints
Auto Layout View
Engine Frames
New
startFrame:
CGRect
startFrame:
CGRect
New
endFrame:
CGRect
@smileyborg
linkedin.com/in/smileyborg
github.com/smileyborg