Top Banner
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC15 Mysteries of Auto Layout, Part 1 Jason Yao Interface Builder Engineer Kasia Wawer iOS Keyboards Engineer App Frameworks Session 218
189

Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Jul 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC15

Mysteries of Auto Layout, Part 1

Jason Yao Interface Builder EngineerKasia Wawer iOS Keyboards Engineer

App Frameworks

Session 218

Page 2: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Auto Layout

Page 3: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Auto Layout

Page 4: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting Started with Auto Layout?

See previous WWDC sessions on http://developer.apple.com/videos/

Page 5: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Part 1 — Morning• Maintainable Layouts• Changing Constraints• View Sizing• Self-Sizing Table View Cells• Priorities • Alignment

Outline

Part 2 — Afternoon• The Layout Cycle• Legacy Layout• Constraint Creation• Constraining Negative Space• Unsatisfiable Constraints• Resolving Ambiguity

Page 6: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Part 1 — Morning• Maintainable Layouts• Changing Constraints• View Sizing• Self-Sizing Table View Cells• Priorities • Alignment

Part 1 — Morning• Maintainable Layouts• Changing Constraints• View Sizing• Self-Sizing Table View Cells• Priorities • Alignment

Outline

Part 2 — Afternoon• The Layout Cycle• Legacy Layout• Constraint Creation• Constraining Negative Space• Unsatisfiable Constraints• Resolving Ambiguity

Page 7: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Maintainable LayoutsMystery #1

Page 8: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

ConstraintsSpacing and alignment

Mysteries of Auto Layout

Page 9: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

ConstraintsSpacing and alignment

Mysteries of Auto LayoutMysteries of Auto Layout

Page 10: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries
Page 11: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Many Constraints

Page 12: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack ViewUIStackView (iOS 9) and NSStackView (OS X 10.9)

stackstacktest | Build stackstacktest: Succeeded | Today at 3:05

Page 13: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View

Page 14: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View

Built with Auto Layout

Page 15: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View

Built with Auto LayoutManages constraints

Page 16: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View

Built with Auto LayoutManages constraintsHorizontal or vertical

Page 17: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack ViewAlignment

Page 18: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack ViewAlignment

Page 19: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack ViewDistribution

Page 20: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries
Page 21: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

DemoStack View in Interface Builder

Page 22: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

// iOS 9 UIView.animateWithDuration(1.0) { () -> Void in self.subviewToHide.hidden = !self.subviewToHide.hidden }

Animate

Page 23: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

// iOS 9 UIView.animateWithDuration(1.0) { () -> Void in self.subviewToHide.hidden = !self.subviewToHide.hidden }

Animate

Page 24: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Animate

// iOS 9 UIView.animateWithDuration(1.0) { self.subviewToHide.hidden = !self.subviewToHide.hidden }

// OS X 10.11 NSAnimationContext.runAnimationGroup({ context in context.duration = 1.0 self.subviewToHide.animator().hidden = !self.subviewToHide.hidden }, completionHandler: nil)

Page 25: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

API

// iOS 9

class UIStackView {

var axis:UILayoutConstraintAxis

var distribution:UIStackViewDistribution

var alignment:UIStackViewAlignment

var spacing:CGFloat

func addArrangedSubview(view: UIView)

var arrangedSubviews:[UIView]

}

// OS X 10.11

class NSStackView {

var orientation:NSUserInterfaceOrientation

var distribution:NSStackViewDistribution

var alignment:NSLayoutAttribute

var spacing:CGFloat

func addArrangedSubview(view: NSView)

var arrangedSubviews:[NSView]

}

Page 26: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

API

// iOS 9

class UIStackView {

var axis:UILayoutConstraintAxis

var distribution:UIStackViewDistribution

var alignment:UIStackViewAlignment

var spacing:CGFloat

func addArrangedSubview(view: UIView)

var arrangedSubviews:[UIView]

}

// OS X 10.11

class NSStackView {

var orientation:NSUserInterfaceOrientation

var distribution:NSStackViewDistribution

var alignment:NSLayoutAttribute

var spacing:CGFloat

func addArrangedSubview(view: NSView)

var arrangedSubviews:[NSView]

}

Page 27: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View in Interface Builder

Page 28: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View in Interface Builder

Easy to build

Page 29: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View in Interface Builder

Easy to buildEasy to maintain

Page 30: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View in Interface Builder

Easy to buildEasy to maintainComposable Stack Views

Page 31: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Stack View in Interface Builder

Easy to buildEasy to maintainComposable Stack ViewsLightweight

Page 32: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Before, many constraints

Before and After

Page 33: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

After, four constraints with Stack View

Before and After

Page 34: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Start with Stack View,use constraints as needed

Page 35: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Feeding the Layout EngineMysteries of Auto Layout, part 1

Kasia Wawer iOS Keyboards Engineer

Page 36: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Page 37: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

???

Page 38: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB Your Layout???

Page 39: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB Your Layout??? ?

Page 40: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

???

Page 41: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Getting from Constraints to Layout

???

Page 42: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Changing ConstraintsMystery #2

Page 43: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Activate and Deactivate

Page 44: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraints find their own container

Activate and Deactivate

Page 45: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraints find their own container

Adds constraints efficiently

Activate and Deactivate

Page 46: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraints find their own container

Adds constraints efficiently

Do not need to own all views

Activate and Deactivate

Page 47: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Activate and Deactivate

Page 48: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add and remove

Activate and Deactivate

Page 49: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Activate and deactivate

Activate and Deactivate

Page 50: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Page 51: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Never deactivate self.view.constraints

Page 52: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Never deactivate self.view.constraints• Not all of those constraints belong to you

Page 53: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Never deactivate self.view.constraints• Not all of those constraints belong to you• Weird things will happen

Page 54: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Never deactivate self.view.constraints• Not all of those constraints belong to you• Weird things will happen• Just don’t do it!

Page 55: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Things to Keep in Mind

Never deactivate self.view.constraints• Not all of those constraints belong to you• Weird things will happen• Just don’t do it!

Keep references to constraints that change

Page 56: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

DemoChanging constraints

Page 57: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Changing Constraints

Page 58: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Changing Constraints

Never deactivate self.view.constraints

Page 59: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Changing Constraints

Never deactivate self.view.constraintsKeep references to constraints

Page 60: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Changing Constraints

Never deactivate self.view.constraintsKeep references to constraintsAnimate changing constraints with view animation

Page 61: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IBLayoutEngine

Page 62: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizingMystery #3

Page 63: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeIntrinsic content size

Label textContent size

Content size

Page 64: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeIntrinsic content size

Certain views have an intrinsicContentSize• For instance–labels and image views Label text

Content size

Content size

Page 65: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeIntrinsic content size

Certain views have an intrinsicContentSize• For instance–labels and image views

Size derived from non-constraint internals Label text

Content size

Content size

Page 66: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeIntrinsic content size

Certain views have an intrinsicContentSize• For instance–labels and image views

Size derived from non-constraint internals System makes the size constraints

Label textContent size

Content size

Page 67: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeIntrinsic content size

Certain views have an intrinsicContentSize• For instance–labels and image views

Size derived from non-constraint internals System makes the size constraintsLayout size is not guaranteed

Label textContent size

Content size

Page 68: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Page 69: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints first

Page 70: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints firstOverride intrinsicContentSize for specific reasons

Page 71: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints firstOverride intrinsicContentSize for specific reasons• If size information does not come from constraints

Page 72: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints firstOverride intrinsicContentSize for specific reasons• If size information does not come from constraints• If view has custom drawing (sometimes)

Page 73: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints firstOverride intrinsicContentSize for specific reasons• If size information does not come from constraints• If view has custom drawing (sometimes)• You will be responsible for invalidating

Page 74: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View SizeDefining a particular view size

Use constraints firstOverride intrinsicContentSize for specific reasons• If size information does not come from constraints• If view has custom drawing (sometimes)• You will be responsible for invalidating

Size can change with size class changes

Page 75: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Example

Page 76: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Width Proportion

ExamplewidthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 0.75, constant: 0.0)

Page 77: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Width Proportion

ExamplewidthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 0.75, constant: 0.0)

Page 78: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Height Proportion

ExamplewidthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 0.75, constant: 0.0)

heightConstraint = NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: imageView, attribute: .Width, multiplier: 1.5, constant: 0.0)

Page 79: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Height Proportion

ExamplewidthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 0.75, constant: 0.0)

heightConstraint = NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: imageView, attribute: .Width, multiplier: 1.5, constant: 0.0)

Page 80: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View CellsMystery #4

Page 81: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Page 82: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraints

Page 83: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cells

Page 84: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cellsConstraints must determine height

Page 85: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cellsConstraints must determine height• Take advantage of proportions

Page 86: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cellsConstraints must determine height• Take advantage of proportions

Page 87: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cellsConstraints must determine height• Take advantage of proportions

Page 88: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Self-Sizing Table View Cells

Self-sizing needs size from constraintsWidth is defined with table view cellsConstraints must determine height• Take advantage of proportions

Page 89: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

DemoSelf-sizing Table View Cells

Page 90: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View Sizing

Page 91: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View Sizing

Certain views have an intrinsicContentSize

Page 92: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View Sizing

Certain views have an intrinsicContentSizeConstraints should define size when possible

Page 93: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

View Sizing

Certain views have an intrinsicContentSizeConstraints should define size when possibleFor self-sizing views, define size fully in constraints

Page 94: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IBLayoutEngine

Page 95: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Sizing constraints

LayoutEngine

Page 96: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

intrinsicContentSize

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Sizing constraints

LayoutEngine

Page 97: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

PrioritiesMystery #5

Page 98: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

Page 99: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution

Page 100: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution• Not enough constraints

Page 101: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution• Not enough constraints

Page 102: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution• Not enough constraints

Page 103: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution• Not enough constraints• Equal, non-required priorities

Page 104: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AmbiguityWhy does it happen?

More than one layout solution• Not enough constraints• Equal, non-required prioritiesThe engine makes a choice

Page 105: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Page 106: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000

Page 107: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250

Page 108: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250

@"H:|-[image]-|" @"V:|-[image]-[caption(==image@751)]-|"

Page 109: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250

@"H:|-[image]-|" @"V:|-[image]-[caption(==image@751)]-|"

Page 110: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250

@"H:|-[image]-|" @"V:|-[image]-[caption(==image@751)]-|"

widthConstraint.priority = UILayoutPriorityDefaultHigh + 10;

Page 111: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250Highest priority wins

@"H:|-[image]-|" @"V:|-[image]-[caption(==image@751)]-|"

widthConstraint.priority = UILayoutPriorityDefaultHigh + 10;

Page 112: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Constraint Priorities

Priorities go from 1–1000 Required is 1000DefaultHigh is 750DefaultLow is 250Highest priority winsSystem uses some priorities• Set around, not equal to

@"H:|-[image]-|" @"V:|-[image]-[caption(==image@751)]-|"

widthConstraint.priority = UILayoutPriorityDefaultHigh + 10;

Page 113: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

Page 114: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its content

Page 115: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Page 116: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguity

Page 117: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer send answer

Page 118: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer send answer

Content hugging priorities

250 250

Page 119: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

Content hugging priorities

250 250

answer send answer

Page 120: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer send answer

Content hugging priorities

250 249

Page 121: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer send answer

Content hugging priorities

250 249

send answer

Page 122: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer

Content hugging priorities

250 249

send answer

251

Page 123: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

249

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

answer

Content hugging priorities

250 251

send answer

Page 124: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

answer

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

Content hugging priorities

250 251

send answer

Page 125: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

answer

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging

Content hugging priorities

250 251

send answer

Page 126: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging• Compression resistance

Page 127: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

red

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging• Compression resistance

Compression resistance priorities

750 750

Page 128: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

czerw

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging• Compression resistance

Compression resistance priorities

750 750

Page 129: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

czerwony

Content Priorities

How a view handles its contentBy default, these are not set as required• Do not set as required• Can cause unsatisfiable constraints

Equal priorities can cause ambiguityTypes• Content hugging• Compression resistance

Compression resistance priorities

750 751

Page 130: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Page 131: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability

Page 132: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability • But look out for competing priorities!

Page 133: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability • But look out for competing priorities!

Results are more consistent

Page 134: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability • But look out for competing priorities!

Results are more consistentUse content priorities to get to the right layout

Page 135: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability • But look out for competing priorities!

Results are more consistentUse content priorities to get to the right layout• Hugging priorities hug content

Page 136: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Priorities

Can help keep constraints from unsatisfiability • But look out for competing priorities!

Results are more consistentUse content priorities to get to the right layout• Hugging priorities hug content• Compression resistance resists squishing

Page 137: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

LayoutEngine

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Sizing constraints

intrinsicContentSize

Page 138: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

LayoutEngine

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Sizing constraintsConstraint priorities

intrinsicContentSize

Page 139: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

LayoutEngine

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

Sizing constraintsConstraint priorities Content priorities

intrinsicContentSize

Page 140: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

AlignmentMystery #6

Page 141: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Aligning Baselines

Page 142: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Aligning Baselines

Use firstBaseline and lastBaseline

Page 143: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottom

Page 144: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Page 145: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Button

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom

Page 146: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

ButtonAdd second line of textand a third while we’re at it

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom

Page 147: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add second line of textand a third while we’re at it

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom

Button

Page 148: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add second line of textand a third while we’re at it

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom

Button

Page 149: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add second line of textand a third while we’re at it

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom

Button

Page 150: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add second line of textand a third while we’re at it

Aligning Baselines

Use firstBaseline and lastBaselineAligns text better than top or bottomBetter control over changing views

Label aligned to button by bottom Button

Page 151: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add commentRate:

One of the best photosI took on the trip.

I love how the water looks like mist!

Leading and Trailing

Page 152: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Add commentRate:

One of the best photosI took on the trip.

I love how the water looks like mist!

Leading and Trailing

Use leading/trailing instead of left/rightHelps with prep for localization

Page 153: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

:امتیازپیشنهاد دهید

یکی از بهترین عکس.هایی که در سفر گرفتم

حالت آب را که شبیه مه!است خیلی دوست دارم

Leading and Trailing

Use leading/trailing instead of left/rightHelps with prep for localization

Tied to left and right

Page 154: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

پیشنهاد دهید امتیاز:

یکی از بهترین عکس.هایی که در سفر گرفتم

حالت آب را که شبیه مه!است خیلی دوست دارم

یکی از بهترین عکس هایی که در سفر گرفتم.

حالت آب را که شبیه مه است خیلی دوست دارم!

Leading and Trailing

Use leading/trailing instead of left/rightHelps with prep for localization

Tied to left and rightTied to leading and trailing

Page 155: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Page 156: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frame

Page 157: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frameIncludes the critical content only

Page 158: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformed

Page 159: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformed

Page 160: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if needed

Page 161: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Tap to choose character

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if neededFind out the calculated rects

Page 162: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if neededFind out the calculated rects• Use Show Alignment Rectangles

in Debug menu

Page 163: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if neededFind out the calculated rects• Use Show Alignment Rectangles

in Debug menu

Page 164: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if neededFind out the calculated rects• Use Show Alignment Rectangles

in Debug menu• Get using alignmentRectForFrame:

Page 165: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment Rects

Usually (not always) same as frameIncludes the critical content onlyDoes not change when view is transformedOverride alignmentRectInsets if neededFind out the calculated rects• Use Show Alignment Rectangles

in Debug menu• Get using alignmentRectForFrame:

More in Part 2

Page 166: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment

Page 167: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment

First and last baseline for better aligned text

Page 168: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment

First and last baseline for better aligned textLeading and trailing instead of left and right

Page 169: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Alignment

First and last baseline for better aligned textLeading and trailing instead of left and rightOverride alignmentRectInsets to adjust alignment rects

Page 170: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

intrinsicContentSize

Sizing constraintsConstraint priorities Content priorities

LayoutEngine

Page 171: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

intrinsicContentSize

Sizing constraintsConstraint priorities Content priorities

Vertical alignment

Baseline alignment

Horizontal alignment

LayoutEngine

Page 172: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Views

Constraints in VFL

Explicit constraints

Constraints in IB

intrinsicContentSize

Sizing constraintsConstraint priorities Content priorities

Vertical alignment

Baseline alignment

Horizontal alignment

Alignment rect insets

LayoutEngine

Page 173: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

LayoutEngine

Page 174: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

LayoutEngine

Page 175: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

The EngineAlignment rects

Building the Layout

LayoutEngine

Page 176: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

The EngineAlignment rects

Building the Layout

Your LayoutLayoutEngine

Page 177: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Building the Layout

Your Layout

Page 178: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Page 179: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layouts

Page 180: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraints

Page 181: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraintsDetermine size through constraints

Page 182: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraintsDetermine size through constraints• Override intrinsicContentSize judiciously

Page 183: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraintsDetermine size through constraints• Override intrinsicContentSize judiciously

Use priorities to properly solve your layout

Page 184: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraintsDetermine size through constraints• Override intrinsicContentSize judiciously

Use priorities to properly solve your layoutAlignment goes beyond top, bottom, and center

Page 185: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Summary

Stack Views help build easily maintainable layoutsUse activate and deactivate for constraintsDetermine size through constraints• Override intrinsicContentSize judiciously

Use priorities to properly solve your layoutAlignment goes beyond top, bottom, and center• Keep localization in mind

Page 186: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

More Information

Documentation and VideosSwift Language Documentationhttp://developer.apple.com/swift

Technical SupportApple Developer Forumshttp://devforums.apple.com

Sample CodeAstroLayouthttp://developer.apple.com/library/prerelease/ios/samplecode/AstroLayout

General InquiriesPaul Marcos, App Frameworks [email protected]

Page 187: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Related Sessions

Mysteries of Auto Layout, Part 2 Presidio Thursday 1:30PM

What’s New in Cocoa Presidio Tuesday 1:30PM

What’s New in UIKit Dynamics and Visual Effects Mission Friday 10:00AM

Cocoa Touch Best Practices Presidio Friday 1:30PM

What’s New in Internationalization Pacific Heights Friday 9:00AM

New UIKit Support for International User Interfaces Nob Hill Thursday 2:30PM

Page 188: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries

Labs

Interface Builder and Auto Layout Lab Developer Tools Lab C Thursday 2:30PM

Page 189: Mysteries of Auto Layout, Part 1 - Apple Inc. · • The Layout Cycle • Legacy Layout • Constraint Creation • Constraining Negative Space ... Feeding the Layout Engine Mysteries