Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
268 slides
-
These are confidential sessions—please refrain from streaming, blogging, or taking
pictures for OS X and iOS Session 228 Peter Ammon AppKit Engineer Best Practices for Mastering Auto Layout -
•One new class—NSLayoutConstraint •Constraints express geometric properties of views •Constraints
also express geometric relationships between views NSLayoutConstraint foo foo.width = 120 -
•One new class—NSLayoutConstraint •Constraints express geometric properties of views •Constraints
also express geometric relationships between views NSLayoutConstraint foo foo.width = 120 foo foo.width = bar.width bar -
•One new class—NSLayoutConstraint •Constraints express geometric properties of views •Constraints
also express geometric relationships between views NSLayoutConstraint foo foo.width = 120 foo foo.width = bar.width bar •Relationships have a coefficient and a constant -
•One new class—NSLayoutConstraint •Constraints express geometric properties of views •Constraints
also express geometric relationships between views NSLayoutConstraint foo foo.width = 120 foo foo.width = bar.width bar •Relationships have a coefficient and a constant foo.width = bar.width * 2 - 20 -
NSLayoutConstraint •Constraints can be equalities or inequalities foo foo.width >=
120 •Constraints have priorities -
NSLayoutConstraint •Constraints can be equalities or inequalities foo foo.width >=
120 •Constraints have priorities foo foo.width = 120 with priority 500 foo.width = 75 with priority 1000 -
NSLayoutConstraint •Constraints can be created three ways ▪ Interface Builder
▪ Visual format language ▪ Base API •Prefer this order -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Thinking in Constraints •Auto Layout can be used like springs
and struts •But you get the most benefits from a shift in thinking •Let your layouts becomes declarative -
Thinking in Constraints Q W E R T Y padding
containerWidth -
Thinking in Constraints Q W E R T Y Springs
and Struts padding containerWidth -
Thinking in Constraints Q W E R T Y Springs
and Struts padding keyWidth = containerWidth / keyCount - padding * (keyCount + 1) / keyCount containerWidth -
Thinking in Constraints Q W E R T Y Springs
and Struts padding keyOffset = padding + keyIndex * (keyWidth + padding) keyWidth = containerWidth / keyCount - padding * (keyCount + 1) / keyCount containerWidth -
Springs and Struts Thinking in Constraints Q W E R
T Y Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Q.width = container.width / keyCount - padding * (keyCount + 1) / keyCount Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Q.width = container.width / keyCount - padding * (keyCount + 1) / keyCount Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Q.width = container.width / keyCount - padding * (keyCount + 1) / keyCount Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... Relationships -
Thinking in Constraints Auto Layout Q W E R T
Y Q.width = container.width / keyCount - padding * (keyCount + 1) / keyCount Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... Relationships -
Thinking in Constraints Q W E R T Y Auto
Layout Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... -
Thinking in Constraints Q W E R T Y Auto
Layout Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... = -
Thinking in Constraints Q W E R T Y Auto
Layout Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... = Q.width = W.width W.width = E.width ... -
Thinking in Constraints Q W E R T Y Auto
Layout Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... = Q.width = W.width W.width = E.width ... -
Thinking in Constraints Q W E R T Y Auto
Layout Q.minX = container.minX + padding W.minX = Q.maxX + padding E.minX = W.maxX + padding... = Q.width = W.width W.width = E.width ... container.maxX = Y.maxX + padding -
•Layout becomes distributed •Decompose sophisticated layouts into components •Each component
contributes the constraints it cares about •Layout becomes “owned” Thinking in Constraints -
•Layout becomes distributed •Decompose sophisticated layouts into components •Each component
contributes the constraints it cares about •Layout becomes “owned” Thinking in Constraints Q -
•Layout becomes distributed •Decompose sophisticated layouts into components •Each component
contributes the constraints it cares about •Layout becomes “owned” Thinking in Constraints Q I’m square! -
•Layout becomes distributed •Decompose sophisticated layouts into components •Each component
contributes the constraints it cares about •Layout becomes “owned” Thinking in Constraints Q I’m square! -
•Layout becomes distributed •Decompose sophisticated layouts into components •Each component
contributes the constraints it cares about •Layout becomes “owned” Thinking in Constraints Q I’m square! Be centered vertically in me! Be the same height as me! -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
1. Plan your attack •A partial conversion lets you use
Auto Layout just where you need it ▪ Some compatibility issues to be aware of •A full conversion will pay major dividends Migrating from Springs and Struts -
2. Turn on Auto Layout in your nibs Migrating from
Springs and Struts -
2. Turn on Auto Layout in your nibs Migrating from
Springs and Struts -
2. Turn on Auto Layout in your nibs Migrating from
Springs and Struts -
2. Turn on Auto Layout in your nibs Migrating from
Springs and Struts -
2. Turn on Auto Layout in your nibs •Create the
constraints you want in IB •IB will create constraints that reflect your existing layout •Add to or modify them Migrating from Springs and Struts -
3. Turn off autoresizing mask translation for every view you
create programmatically [view setTranslatesAutoresizingMaskIntoConstraints:NO] •If you forget, you’ll get unsatisfiable constraint warnings quickly Migrating from Springs and Struts -
4. Look for places where you perform layout Migrating from
Springs and Struts -
4. Look for places where you perform layout - (void)layoutSubviews
{… Migrating from Springs and Struts -
4. Look for places where you perform layout - (void)layoutSubviews
{… Migrating from Springs and Struts [view setFrame:rect] [view setFrameSize:size] [view setFrameOrigin:point] -
4. Look for places where you perform layout - (void)layoutSubviews
{… Migrating from Springs and Struts [view setFrame:rect] [view setFrameSize:size] [view setFrameOrigin:point] They all have to go! -
4. Look for places where you perform layout - (void)layoutSubviews
{… Migrating from Springs and Struts [view setFrame:rect] [view setFrameSize:size] [view setFrameOrigin:point] They all have to go! (But what do I replace them with?) -
Migrating from Springs and Struts •Stop and think ▪ Don’t
try to merely replicate what the existing code is doing ▪ Think about the underlying layout -
Migrating from Springs and Struts •Stop and think ▪ Don’t
try to merely replicate what the existing code is doing ▪ Think about the underlying layout •Try replacing it with nothing! ▪ Are you working around a limitation of springs and struts? ▪ Does the code implement a relationship? -
Migrating from Springs and Struts •Stop and think ▪ Don’t
try to merely replicate what the existing code is doing ▪ Think about the underlying layout •Try replacing it with nothing! ▪ Are you working around a limitation of springs and struts? ▪ Does the code implement a relationship? •Otherwise, add some constraints -
Migrating from Springs and Struts •Think about which component should
own each constraint -
Migrating from Springs and Struts •Think about which component should
own each constraint •Consider centralizing it in updateConstraints -
5. Test it •Verify the layout is correct •Fix issues
you may have Migrating from Springs and Struts -
5. Test it •Verify the layout is correct •Fix issues
you may have Migrating from Springs and Struts •Wait, what? -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
•Constraints that provide insufficient information ▪ Ambiguity •Constraints that provide
conflicting information ▪ Unsatisfiability •Constraints that are satisfied in unexpected ways What Can Go Wrong? -
•Interface Builder prevents unsatisfiable or ambiguous constraints •Rely on Interface
Builder as much as possible •You can reference constraints with outlets What Can Go Wrong? -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
•Ambiguity means multiple layouts satisfy all constraints equally well Q
W E R T Y Ambiguity -
Ambiguity •Ambiguity means multiple layouts satisfy all constraints equally well
•A common symptom is that your views will cycle between those layouts -
Ambiguity •Ambiguity means multiple layouts satisfy all constraints equally well
•A common symptom is that your views will cycle between those layouts •Views “jump” or disappear entirely (jump to zero size) -
•Usually it means you need more constraints •Each view needs
four properties (two in each dimension) ▪ MinX, Width, MinY, Height ▪ MinX, MaxX, CenterY, MaxY ▪ CenterX, Width, Baseline, Height ▪ etc. Ambiguity -
•Usually it means you need more constraints •Each view needs
four properties (two in each dimension) ▪ MinX, Width, MinY, Height ▪ MinX, MaxX, CenterY, MaxY ▪ CenterX, Width, Baseline, Height ▪ etc. •Inequalities by themselves are usually not enough ▪ view.width ≥ 20 – is it 20? 200? 2 billion? ▪ Inequalities don’t care how much larger or smaller you are ▪ But equalities care Ambiguity -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
can’t satisfy both •They have equal priorities •Ambiguity! [view(24@500) [view(>=30@500)] -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
still can’t satisfy both [view(24@500) [view(>=30@525)] -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
still can’t satisfy both •The inequality has a higher priority, so it will be satisfied first [view(24@500) [view(>=30@525)] -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
still can’t satisfy both •The inequality has a higher priority, so it will be satisfied first •The equality will be satisfied as closely as possible [view(24@500) [view(>=30@525)] -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
still can’t satisfy both •The inequality has a higher priority, so it will be satisfied first •The equality will be satisfied as closely as possible •No ambiguity! [view(24@500) [view(>=30@525)] -
•Rarely, ambiguity means you need to adjust priorities Ambiguity •It
still can’t satisfy both •The inequality has a higher priority, so it will be satisfied first •The equality will be satisfied as closely as possible •No ambiguity! •view.width = 30 [view(24@500) [view(>=30@525)] -
•Is my layout ambiguous? [view hasAmbiguousLayout] •What is ambiguous about
it? [view exerciseAmbiguityInLayout] [window visualizeConstraints: @[] ] Ambiguity -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
•Unsatisfiability means no layout can satisfy all required constraints •Only
required constraints can contribute to unsatisfiability ▪ Constraints are required by default! Unsatisfiability -
•Unsatisfiability means no layout can satisfy all required constraints •Only
required constraints can contribute to unsatisfiability ▪ Constraints are required by default! •Sizes are implicitly required to be at least zero Unsatisfiability -
•Unsatisfiability is immediately reported •Ambiguity can be temporarily tolerated •Remove
constraints as soon as they might become invalid •Create valid constraints again in updateConstraints Unsatisfiability -
Help, I Don’t See Anything! •Where are your views? ▪
Check their -frame -
Help, I Don’t See Anything! •Where are your views? ▪
Check their -frame •What constraints are making them that size? ▪ Output [view constraintsAffectingLayoutForOrientation/Axis: NSLayoutConstraintOrientationHorizontal/Vertical] -
Help, I Don’t See Anything! •Where are your views? ▪
Check their -frame •What constraints are making them that size? ▪ Output [view constraintsAffectingLayoutForOrientation/Axis: NSLayoutConstraintOrientationHorizontal/Vertical] 0 or 1 -
Help, I Don’t See Anything! •Where are your views? ▪
Check their -frame •What constraints are making them that size? ▪ Output [view constraintsAffectingLayoutForOrientation/Axis: NSLayoutConstraintOrientationHorizontal/Vertical] •Is the layout ambiguous? ▪ Call [view hasAmbiguousLayout] ▪ Call [view exerciseAmbiguityInLayout] 0 or 1 -
Help, I Don’t See Anything! •Some layouts are only satisfiable
at 0 size! foo.width = bar.width * 2 bar.width = foo.width * 3 -
Help, I Don’t See Anything! •Some layouts are only satisfiable
at 0 size! foo.width = bar.width * 2 bar.width = foo.width * 3 foo.width = bar.width = 0 -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Unable to simultaneously satisfy constraints: ( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:
0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. Anatomy of an Unsatisfiability Log -
Unable to simultaneously satisfy constraints: ( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:
0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. Anatomy of an Unsatisfiability Log -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log Unable to simultaneously satisfy constraints:
( "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 )>", "<NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x10441ce70 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX + 170 (Names: LetterView-'H':0x10423c390 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. -
Anatomy of an Unsatisfiability Log "<NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile: 0x102b25230.centerX
- 11 (Names: LetterView-'H':0x10423c390 )>", -
Anatomy of an Unsatisfiability Log NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX -
11 (Names: LetterView-'H':0x10423c390 ) -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log Constraint’s address -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log Map from identifier to view -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log Map from identifier to view •View identifiers make logs easier to read -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log Map from identifier to view •View identifiers make logs easier to read -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log Map from identifier to view •View identifiers make logs easier to read -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier Attribute -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier Attribute Relation -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier Attribute Relation Other View -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier Attribute Relation Other View Constant -
NSLayoutConstraint:0x10441ced0 LetterView-'H'.centerX == LetterPile:0x102b25230.centerX - 11 (Names: LetterView-'H':0x10423c390 ) Anatomy
of an Unsatisfiability Log View’s identifier Attribute Relation Other View Constant “The letter view’s center should be 11 points to the left of the pile’s center” -
Anatomy of an Unsatisfiability Log •Logs use the visual format
syntax when possible -
Anatomy of an Unsatisfiability Log •Logs use the visual format
syntax when possible H:[NSView:0x102b5b3a0(250)] -
Anatomy of an Unsatisfiability Log •Logs use the visual format
syntax when possible H:[NSView:0x102b5b3a0(250)] “The view’s width is 250” -
Anatomy of an Unsatisfiability Log •Logs use the visual format
syntax when possible H:[NSView:0x102b5b3a0(250)] H:[NSView:0x10480cd00]-(>=50)-[NSView:0x10481e9a0]> “The view’s width is 250” -
Anatomy of an Unsatisfiability Log •Logs use the visual format
syntax when possible H:[NSView:0x102b5b3a0(250)] H:[NSView:0x10480cd00]-(>=50)-[NSView:0x10481e9a0]> “The view’s width is 250” “This view is at least 50 points to the right of that view” -
Anatomy of an Unsatisfiability Log <NSAutoresizingMaskLayoutConstraint:0x10590a360 h=-&- v=&-- V:[NSView:0x102e2af20(50)]> •
translatesAutoresizingMaskIntoConstraints is on for this view -
Anatomy of an Unsatisfiability Log <NSAutoresizingMaskLayoutConstraint:0x10590a360 h=-&- v=&-- V:[NSView:0x102e2af20(50)]> •
translatesAutoresizingMaskIntoConstraints is on for this view •That produces more than one constraint Autoresizing Mask -
Anatomy of an Unsatisfiability Log <NSAutoresizingMaskLayoutConstraint:0x10590a360 h=-&- v=&-- V:[NSView:0x102e2af20(50)]> •
translatesAutoresizingMaskIntoConstraints is on for this view •That produces more than one constraint Autoresizing Mask -
Anatomy of an Unsatisfiability Log <NSAutoresizingMaskLayoutConstraint:0x10590a360 h=-&- v=&-- V:[NSView:0x102e2af20(50)]> •
translatesAutoresizingMaskIntoConstraints is on for this view •That produces more than one constraint Autoresizing Mask “This view’s height is 50.” -
<NSAutoresizingMaskLayoutConstraint:0x103b25030 h=-&- v=&-- H:|-(200)-[NSView:0x103b25090] (Names: '|':FlippedView:0x102e163f0 )> Horizontal 200 points
between this view’s left edge Superview -
<NSAutoresizingMaskLayoutConstraint:0x103b25030 h=-&- v=&-- H:|-(200)-[NSView:0x103b25090] (Names: '|':FlippedView:0x102e163f0 )> “This view’s left
edge is 200 points from that of its superview, which is a FlippedView” -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint based-layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Animation •How do you animate layout changes? •Apply the new
layout and let CoreAnimation handle animation ▪ Very fast ▪ May transiently appear to violate constraints -
Animation •How do you animate layout changes? •Apply the new
layout and let CoreAnimation handle animation ▪ Very fast ▪ May transiently appear to violate constraints •Animate constraints directly ▪ Pretty fast ▪ Produces a correct layout at every frame -
Animation with CoreAnimation •Adjust your constraints •Within an animation block,
call [view layoutIfNeeded] on iOS [view layoutSubtreeIfNeeded] on OS X -
Animation with CoreAnimation •NSView [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { [context setDuration:0.5];
[context setAllowsImplicitAnimation:YES]; [view layoutSubtreeIfNeeded]; } completionHandler:nil] •UIView [UIView animateWithDuration:0.5 animations:^{ [view layoutIfNeeded]; }] -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts •Use an NSTimer -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts •Use an NSTimer constraint.constant += 10 -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts •Use an NSTimer •Use the animator proxy constraint.constant += 10 -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts •Use an NSTimer •Use the animator proxy constraint.constant += 10 constraint.animator.constant = 10 -
Animation with NSLayoutConstraint @interface NSLayoutConstraint @property (readonly) NSLayoutAttribute firstAttribute; @property
(readonly) CGFloat multiplier; @property (readwrite) CGFloat constant; @end •The constant may be modified after creation •Permits efficient relayouts •Use an NSTimer •Use the animator proxy constraint.constant += 10 constraint.animator.constant = 10 OS X only -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Push Me -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Push Me Push Me Push Me Push Me -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Push Me Push Me Push Me ? ? ? Push Me -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Push Me Frame Alignment Rect -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Edit -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Edit -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Edit 4 -
Alignment Rects •Constraints operate on content, not frames •The content
area is called the alignment rect Edit 4 -
Alignment Rects •You can convert between alignment rects and frames
@implementation (NS,UI)View - (CGRect)alignmentRectForFrame:(CGRect)frame - (CGRect)frameForAlignmentRect:(CGRect)alignmentRect -
Intrinsic Content Size •Many views are equally happy at any
size •Some views have a preferred size sizeToFit sizeThatFits: •In Auto Layout, this is the intrinsicContentSize -
Intrinsic Content Size •Many views are equally happy at any
size •Some views have a preferred size sizeToFit sizeThatFits: •In Auto Layout, this is the intrinsicContentSize Push Me -
Intrinsic Content Size •Many views are equally happy at any
size •Some views have a preferred size sizeToFit sizeThatFits: •In Auto Layout, this is the intrinsicContentSize Push Me -
Intrinsic Content Size •Many views are equally happy at any
size •Some views have a preferred size sizeToFit sizeThatFits: •In Auto Layout, this is the intrinsicContentSize Push Me ? -
Intrinsic Content Size •An intrinsic content size generates two constraints
per dimension Push Me 120 25 H:[button(>=120] H:[button(<=120] V:[button(>=25)] V:[button(<=25)] -
Intrinsic Content Size •An intrinsic content size generates two constraints
per dimension Push Me 120 25 H:[button(>=120] H:[button(<=120] V:[button(>=25)] V:[button(<=25)] Compression Resistance -
Intrinsic Content Size •An intrinsic content size generates two constraints
per dimension Push Me 120 25 H:[button(>=120] H:[button(<=120] V:[button(>=25)] V:[button(<=25)] Compression Resistance Content Hugging -
Intrinsic Content Size •An intrinsic content size generates two constraints
per dimension Push Me 120 25 H:[button(>=120] H:[button(<=120] V:[button(>=25)] V:[button(<=25)] •This is sufficient to unambiguously size the view! Compression Resistance Content Hugging -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! This is a label -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! This is a label This is a label -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! This is a label This is… This is a label -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! This is a label This is… This is a label •Low content hugging priority, high compression resistance priority -
Intrinsic Content Size •Why two constraints? •Because they can have
different priorities! •High content hugging priority, high compression resistance priority Push Me Push Me Pus… -
Intrinsic Content Size •Intrinsic content size is not settable •The
constraint priorities are settable -
Intrinsic Content Size •Intrinsic content size is not settable •The
constraint priorities are settable -
Intrinsic Content Size •Intrinsic content size is not settable •The
constraint priorities are settable -
Intrinsic Content Size •Intrinsic content size is not settable •The
constraint priorities are settable @implementation NSView - (void)setContentHuggingPriority:(NSLayoutPriority)priority forOrientation:(NSLayoutConstraintOrientation)orientation; - (void)setContentCompressionResistancePriority:(NSLayoutPriority)priority forOrientation:(NSLayoutConstraintOrientation)orientation; @end -
Intrinsic Content Size •Intrinsic content size is not settable •The
constraint priorities are settable @implementation UIView - (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis; - (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis; @end -
Intrinsic Content Size •The view calls [self invalidateIntrinsicContentSize] whenever its
content changes •Auto Layout reestablishes the sizing constraints •If you implement a custom control, call this whenever your intrinsicContentSize might change Short Label Longer L… -
Intrinsic Content Size •The view calls [self invalidateIntrinsicContentSize] whenever its
content changes •Auto Layout reestablishes the sizing constraints •If you implement a custom control, call this whenever your intrinsicContentSize might change Short Label Longer L… Longer Longer Label -
Intrinsic Content Size intrinsicContentSize as a better sizeToFit •sizeToFit must
preserve binary compatibility •It may be wrong for the current artwork •intrinsicContentSize can change •Use intrinsicContentSize as a better sizeToFit -
Intrinsic Content Size intrinsicContentSize as a better sizeToFit •sizeToFit must
preserve binary compatibility •It may be wrong for the current artwork •intrinsicContentSize can change •Use intrinsicContentSize as a better sizeToFit NSRect alignmentRect = (NSRect){NSZeroPoint, [control intrinsicContentSize]}; [control setFrameSize: [control frameForAlignmentRect:alignmentRect].size]; -
Override intrinsicContentSize Writing a Custom Control •Do ▪ Measure text
or images ▪ Hard-code values -
Override intrinsicContentSize Writing a Custom Control •Do ▪ Measure text
or images ▪ Hard-code values •Do not ▪ Inspect your position, size, or constraints ▪ Call super and “tweak” its value ▪ Use it as a substitute for explicit constraints -
Indicate your alignment rect Writing a Custom Control •Do ▪
Consider using the default implementation ▪ Override - (NS/UIEdgeInsets)alignmentRectInsets; -
Indicate your alignment rect Writing a Custom Control •Do ▪
Consider using the default implementation ▪ Override - (NS/UIEdgeInsets)alignmentRectInsets; •Do not ▪ Inspect your position, size, or constraints ▪ Call super and “tweak” its value ▪ Use it as a substitute for explicit constraints -
Overriding layout / layoutSubviews -layout sets the receiver’s frame to
the values determined by the constraints NSView -
Overriding layout / layoutSubviews -layout sets the receiver’s frame to
the values determined by the constraints NSView -layoutSubviews sets the receiver’s center and bounds to the values determined by the constraints UIView -
Overriding layout / layoutSubviews •Afterwards, the constraints and frames agree
•Override it to do custom layout as long as you maintain that invariant -layout sets the receiver’s frame to the values determined by the constraints NSView -layoutSubviews sets the receiver’s center and bounds to the values determined by the constraints UIView -
Overriding layout / layoutSubviews Achieving a layout-dependent view hierarchy •Override
-layout / -layoutSubviews •Call super •Inspect the resulting view positions and sizes •Adjust subviews and constraints •Call super again •Repeat! -
Auto Layout •Thinking in constraints ▪ Transitioning to constraints •Debugging
constraint-based layouts ▪ Ambiguity ▪ Unsatisfiability ▪ Reading log messages •Unleashing the power of constraints ▪ Animation ▪ Writing a custom control ▪ Internationalization -
Internationalization •Auto Layout makes internationalization easier ▪ Controls size according
to their content ▪ The same constraints still work across different localizations -
Internationalization •Auto Layout makes internationalization easier ▪ Controls size according
to their content ▪ The same constraints still work across different localizations Save Cancel -
Internationalization •Auto Layout makes internationalization easier ▪ Controls size according
to their content ▪ The same constraints still work across different localizations Sichern Abbrechen -
Internationalization •One nib can now service multiple localizations •Control content
is translated with a strings file at runtime •You can fall back to separate nibs when necessary Sichern Abbrechen -
Internationalization •Right-to-left support is built in •The leading and trailing
edges flip under right-to-left localizations Sichern Abbrechen -
!"# ء%&'إ Internationalization •Right-to-left support is built in •The leading
and trailing edges flip under right-to-left localizations Sichern Abbrechen -
Summary •Auto Layout allows for powerful layout with less (or
no) code •Think declaratively •Be wary of ambiguity and unsatisfiability •The log messages are there to help •Judicious overriding lets your custom views integrate with Auto Layout •Localize with a single nib and multiple strings files -
More Information Paul Marcos Frameworks Evangelist [email protected] Documentation Cocoa Auto
Layout Guide http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/ Apple Developer Forums http://devforums.apple.com