SlideShare a Scribd company logo
Auto-Layout in iOS 7
Layout 
Layout Consist of four things: 
· Center 
· Bounds 
· Auto-resizing mask 
· Transform
Basic Concept 
Expression of the form y = m*x + b, where: 
y and x are attributes of views. 
m and b are floating point values. 
An attribute is one of left, right, top, bottom, 
leading, trailing, width, height, centerX, centerY, 
and baseline.
constraints 
Layout NSLayoutConstraint 
Content NSContentSizeLayoutConstraint 
AutosizingNSAutoresizingMaskLayoutConstraint
Constraints Attribute 
view1.attribute1 RELATION multiplier * view2.attribute2 + constant 
NSLayoutAttributeLeft 
NSLayoutAttributeRight 
NSLayoutAttributeTop 
NSLayoutAttributeBottom 
NSLayoutAttributeLeading 
NSLayoutAttributeTrailing 
NSLayoutAttributeWidth 
NSLayoutAttributeHeight 
NSLayoutAttributeCenterX 
NSLayoutAttributeCenterY 
NSLayoutAttributeBaseline 
NSLayoutRelationEqual 
NSLayoutRelationGreaterThanOrEqual 
NSLayoutRelationLessThanOrEqual 
NSLayoutAttributeNotAnAttribute
Autolayout in iOS
Constraints Code 
view1.attribute1 RELATION multiplier * view2.attribute2 + constant 
constraint = [NSLayoutConstraint 
constraintWithItem: view 
attribute: NSLayoutAttributeWidth 
relatedBy: NSLayoutRelationEqual 
toItem: nil 
attribute: NSLayoutAttributeNotAnAttribute 
multiplier: 1.0 
constant:100.0]; 
[view addConstraint: constraint]; 
constraint = [NSLayoutConstraint 
constraintWithItem: view 
attribute: NSLayoutAttributeWidth 
relatedBy: NSLayoutRelationEqual 
toItem: nil 
attribute: NSLayoutAttributeNotAnAttribute 
multiplier: 1.0 
Constant: 80.0]; 
[view addConstraint: constraint];
Check For Ambiguous Layout 
view.hasAmbiguousLayout 
view.exerciseAmbiguityInLayout 
for (UIView *view in self.subviews) { 
if ([view hasAmbiguousLayout]){ 
NSLog(@"<%@:0x%0x>", view.description, (int)self); 
} 
}
Intrinsic ContentSize 
Suggested size for the view. 
- (CGSize) intrinsicContentSize { 
return mySize; 
} 
[self invalidateIntrinsicContentSize]; 
UIImage *img = UIImage imageNamed:@"Icon.png"]; 
UIImageView *iv = [[UIImageView alloc] initWithImage:img]; 
NSLog(@"%@", NSStringFromCGSize(iv.intrinsicContentSize));
Visual Format Language 
[NSLayoutConstraint 
! constraintsWithVisualFormat:@"H:|-[buttonA]-|" ! options:0 
! metrics:nil 
! views:@{@"buttonA":buttonA}];
UIView 
Opting in to Constraint-Based Layout 
+ requiresConstraintBasedLayout 
– translatesAutoresizingMaskIntoConstraints 
– setTranslatesAutoresizingMaskIntoConstraints: 
Managing Constraints 
– constraints 
– addConstraint: 
– addConstraints: 
– removeConstraint: 
– removeConstraints: 
Measuring in Constraint-Based Layout 
– systemLayoutSizeFittingSize 
– intrinsicContentSize 
– invalidateIntrinsicContentSize 
– contentCompressionResistancePriorityForAxis: 
– setContentCompressionResistancePriority:forAxis: 
– contentHuggingPriorityForAxis: 
– setContentHuggingPriority:forAxis:
UIView 
Aligning Views with Constraint-Based Layout 
– alignmentRectForFrame: 
– frameForAlignmentRect: 
– alignmentRectInsets 
– viewForBaselineLayout 
Triggering Constraint-Based Layout 
– needsUpdateConstraints 
– setNeedsUpdateConstraints 
– updateConstraints 
– updateConstraintsIfNeeded 
Debugging Constraint-Based Layout 
– constraintsAffectingLayoutForAxis: 
– hasAmbiguousLayout 
– exerciseAmbiguityInLayout
Queries
Autolayout in iOS

More Related Content

PDF
Autolayout primer
PDF
Autolayout
PDF
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
PDF
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
PDF
UI Dynamics
PDF
Standford 2015 week6
PPTX
Introduction to auto layout
Autolayout primer
Autolayout
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
UI Dynamics
Standford 2015 week6
Introduction to auto layout

Similar to Autolayout in iOS (20)

PDF
Auto-Layout in iOS 7
PDF
Autolayout
PDF
Auto Layout Under Control @ Pragma conference 2013
PDF
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
PDF
Auto Layout
PDF
Manual Layout Revisited
PDF
iOS Layout Overview
PPTX
Auto Layouts , NSLayoutConstraint, Traits in iOS
KEY
Opening iOS App 開發者交流會
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 06)
PDF
Lviv MD Day 2015 Костянтин Бичков "iOS Autolayouts – це добре!"
PDF
Auto Layout on Xcode 5
PDF
Understanding Auto Layout
PDF
iOS Design to Code - Design
PPT
Introduction to Auto-layout : iOS/Mac
PDF
iOS 上 self-sizing cell 的過去、現在、與未來
PDF
Practical auto layout
PDF
Auto Layout Path to Mastery - Lightning Talk
PDF
Auto Layout part 1
PDF
Auto Layout Priorities - CocoaConf 2016 Seattle
Auto-Layout in iOS 7
Autolayout
Auto Layout Under Control @ Pragma conference 2013
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
Auto Layout
Manual Layout Revisited
iOS Layout Overview
Auto Layouts , NSLayoutConstraint, Traits in iOS
Opening iOS App 開發者交流會
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 06)
Lviv MD Day 2015 Костянтин Бичков "iOS Autolayouts – це добре!"
Auto Layout on Xcode 5
Understanding Auto Layout
iOS Design to Code - Design
Introduction to Auto-layout : iOS/Mac
iOS 上 self-sizing cell 的過去、現在、與未來
Practical auto layout
Auto Layout Path to Mastery - Lightning Talk
Auto Layout part 1
Auto Layout Priorities - CocoaConf 2016 Seattle
Ad

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Project quality management in manufacturing
PPTX
Welding lecture in detail for understanding
PPTX
Geodesy 1.pptx...............................................
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
web development for engineering and engineering
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
additive manufacturing of ss316l using mig welding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Project quality management in manufacturing
Welding lecture in detail for understanding
Geodesy 1.pptx...............................................
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
web development for engineering and engineering
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
bas. eng. economics group 4 presentation 1.pptx
Digital Logic Computer Design lecture notes
Operating System & Kernel Study Guide-1 - converted.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
additive manufacturing of ss316l using mig welding
Ad

Autolayout in iOS

  • 2. Layout Layout Consist of four things: · Center · Bounds · Auto-resizing mask · Transform
  • 3. Basic Concept Expression of the form y = m*x + b, where: y and x are attributes of views. m and b are floating point values. An attribute is one of left, right, top, bottom, leading, trailing, width, height, centerX, centerY, and baseline.
  • 4. constraints Layout NSLayoutConstraint Content NSContentSizeLayoutConstraint AutosizingNSAutoresizingMaskLayoutConstraint
  • 5. Constraints Attribute view1.attribute1 RELATION multiplier * view2.attribute2 + constant NSLayoutAttributeLeft NSLayoutAttributeRight NSLayoutAttributeTop NSLayoutAttributeBottom NSLayoutAttributeLeading NSLayoutAttributeTrailing NSLayoutAttributeWidth NSLayoutAttributeHeight NSLayoutAttributeCenterX NSLayoutAttributeCenterY NSLayoutAttributeBaseline NSLayoutRelationEqual NSLayoutRelationGreaterThanOrEqual NSLayoutRelationLessThanOrEqual NSLayoutAttributeNotAnAttribute
  • 7. Constraints Code view1.attribute1 RELATION multiplier * view2.attribute2 + constant constraint = [NSLayoutConstraint constraintWithItem: view attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant:100.0]; [view addConstraint: constraint]; constraint = [NSLayoutConstraint constraintWithItem: view attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 Constant: 80.0]; [view addConstraint: constraint];
  • 8. Check For Ambiguous Layout view.hasAmbiguousLayout view.exerciseAmbiguityInLayout for (UIView *view in self.subviews) { if ([view hasAmbiguousLayout]){ NSLog(@"<%@:0x%0x>", view.description, (int)self); } }
  • 9. Intrinsic ContentSize Suggested size for the view. - (CGSize) intrinsicContentSize { return mySize; } [self invalidateIntrinsicContentSize]; UIImage *img = UIImage imageNamed:@"Icon.png"]; UIImageView *iv = [[UIImageView alloc] initWithImage:img]; NSLog(@"%@", NSStringFromCGSize(iv.intrinsicContentSize));
  • 10. Visual Format Language [NSLayoutConstraint ! constraintsWithVisualFormat:@"H:|-[buttonA]-|" ! options:0 ! metrics:nil ! views:@{@"buttonA":buttonA}];
  • 11. UIView Opting in to Constraint-Based Layout + requiresConstraintBasedLayout – translatesAutoresizingMaskIntoConstraints – setTranslatesAutoresizingMaskIntoConstraints: Managing Constraints – constraints – addConstraint: – addConstraints: – removeConstraint: – removeConstraints: Measuring in Constraint-Based Layout – systemLayoutSizeFittingSize – intrinsicContentSize – invalidateIntrinsicContentSize – contentCompressionResistancePriorityForAxis: – setContentCompressionResistancePriority:forAxis: – contentHuggingPriorityForAxis: – setContentHuggingPriority:forAxis:
  • 12. UIView Aligning Views with Constraint-Based Layout – alignmentRectForFrame: – frameForAlignmentRect: – alignmentRectInsets – viewForBaselineLayout Triggering Constraint-Based Layout – needsUpdateConstraints – setNeedsUpdateConstraints – updateConstraints – updateConstraintsIfNeeded Debugging Constraint-Based Layout – constraintsAffectingLayoutForAxis: – hasAmbiguousLayout – exerciseAmbiguityInLayout