SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
Autolayout Primer
Internal presentation @ iCapps, 2013-12-04
Introduction
Tom Adriaenssen
I love...
‣ ... my wife
‣ ... my 4 kids
‣ ... to code
‣ ... to play a game of squash
‣ ... good beer
I open sourced...
... some code:
‣ IIViewDeckController: “An
implementation of the
sliding functionality found in
the Path 2.0 or Facebook
iOS apps.”
‣ IIDateExtensions
‣ IIPopoverStatusItem
See: http://github.com/inferis
I made...
... some apps:

Butane
http://getbutane.com
Hi, @10to1!

Drash

http://dra.sh
Agenda
Agenda
‣ What is Autolayout?
‣ What is a constraint?
‣ Autolayout in Xcode designer
‣ Autolayout in code
Autolayout
What is Autolayout?
According to the Apple Docs:
Auto%Layout%is%a%system%that%lets%you%lay%out%your%app’s%user%interface%by%
creating%a%mathematical%description%of%the%relationships%between%the%
elements.%%

‣

Keywords:
‣
‣

‣

mathematical
relationships

Eh?
‣

Developer defines constraints on/between elements

‣

constraints translate into mathematical functions

‣

UIKit/Appkit tries to solve these functions
What is Autolayout?
‣

Correctly using autolayout requires you to change thinking
‣

Autolayout works in a different way than what you were
used to

‣

Don't go calculating frames anymore

‣

Define how an element should behave according to it's
peers:
‣

parent

‣

siblings

‣

Don't think in absolute terms, but think in relational terms

‣

Think flexible: it's not because an element has a certain
size at one point, it will have the same size at another point
Constraints
Constraint
a%mathematical%representation%of%a%human:expressable%statement%

"the left edge should be 20pt of the
superviews left edge"
view.left = (superview.left + 20)
y=m*x+c
(y%=%attribute1%=%view.left)%
(x%=%attribute2%=%superview.left)%
(m%=%multiplier%=%1)%
(c%=%constant%=%20)
Constraint
‣

two items with an attribute

‣

constant

‣

relation

‣

multiplier

‣

priority level

//%firstItem.firstAttribute%{==,<=,>=}%secondItem.secondAttribute%*%multiplier%+%constant%

!

@property%(readonly,%assign)%id%firstItem;%
@property%(readonly)%NSLayoutAttribute%firstAttribute;%
@property%(readonly)%NSLayoutRelation%relation;%
@property%(readonly,%assign)%id%secondItem;%
@property%(readonly)%NSLayoutAttribute%secondAttribute;%
@property%(readonly)%CGFloat%multiplier;%

!

@property%CGFloat%constant;
Attribute?
‣

left

‣

right

‣

top

‣

bottom

‣

leading ~ left or right depending on language settings

‣

trailing ~ right or left depending on language settings

‣

width

‣

height

‣

centerX

‣

centerY

‣

baseline
Constant?
‣

physical size or offset
Multiplier?
‣

Apply modifier to attribute of source item
‣

Cannot be set using the designer

‣

Can be set in code
Relation?
‣

equal

‣

less than or equal

‣

greater than or equal
!

‣

if you want just "less than" or "greater than",
adjust the constant
Priority
‣

constraints with higher priority are satisfied before
constraints with lower priority

‣

	Default = UILayoutPriorityRequired (1000)

‣

	Other predefined priorities
‣

	 UILayoutPriorityRequired = 1000

‣

	 UILayoutPriorityDefaultHigh = 750
‣

‣

	 UILayoutPriorityDefaultLow = 250
‣

‣
‣

Default content compression priority

Default content hugging priority

	 UILayoutPriorityFittingSizeLevel = 50

You can use any integer value between 0-1000
Pitfalls
‣

constraints are cumulative

‣

constraints do not override each other
‣

‣
‣

adding a second width constraint does not
remove or override a previous one
remove first one manually

constraints can cross view hierarchy
‣

add constraint from view to superview of
superview

‣

only if scope of view hierarchy uses autolayout!
(no custom framesetting)
Intrisic Content Size
‣

applicable to leaf views (views with no subviews)

‣

view knows what size it wants to be (eg UIButton)

‣

autolayout will size view to its intristic content size if no
constraints control the width/height
:%(CGSize)intrinsicContentSize;
Returns%the%natural%size%for%the%receiving%view,%considering%
only%properties%of%the%view%itself.%

‣

You can override this method in a subclass to change the default
behavior

‣

	return UIViewNoIntrinsicMetric when there's no intristic size defined
for an axis
Compression resistance &
Content hugging
‣

Content Compression resistance?
‣

‣

defines how the view's frame resist
compressing it's content

Content hugging?
‣

defines how the view's frame should hug it's
content
Compression resistance & Content hugging
Say you've got button like this:
%

|[%%%%%%%Click%Me%%%%%%]|%

and you've pinned the edges to a larger superview with priority 500.

!
Then, if hugging priority > 500 it'll look like this:
%

|[Click%Me]%%%%%%%%%%%%%|%

If hugging priority < 500 it'll look like this:
%

|[%%%%%%%Click%Me%%%%%%]|%

!
If the superview now shrinks and if the compression resistance priority > 500,
it'll look like this
%

|[Click%|Me]%

else if compression resistance priority < 500, it could look like this:
%

|[Cli..]|
Autolayout in
Xcode designer
Demo
Autolayout in code
Autolayout in code: 3 options
‣

Write constraints manually
‣

‣

Use visual format
‣

‣

HEAD WILL EXPLODE
HEAD WILL EXPLODE MOAR

Use UIView+AutoLayout Cocoapod
‣

Provides descriptive methods creating
constraints

‣

Still verbose but quite readable

‣

Best of both worlds
Autolayout in code
‣

Important note when creating constraints in code

‣

If you create a view manually, use:
!

view%=%[UIView%new];%
view.translatesAutoresizingMaskIntoConstraints%=%NO;

!
‣

if you don’t, you’ll get a lot of constraint errors!
UIView+AutoLayout
‣

Available as a CocoaPod
‣

Implements a category on UIView.

‣

Easy creation of 1 or more NSLayoutConstraints.

‣

Some methods return 1 constraint, some an NSArray
containing constraints

‣

Does not add constraints, just creates them
‣

	you need to add them yourself
:(void)addConstraint:(NSLayoutConstraint%*)constraint;%
:(void)addConstraints:(NSArray%*)constraints;
UIView+AutoLayout
Creating an autolayouted view
‣

Don't use: translatesAutoresizingMaskIntoConstraints

!
%
%
%

view%=%[UIView%newAutoLayoutView];%
//%or%
view%=%[[UIView%alloc]%initForAutoLayout];
UIView+AutoLayout
Pin to superview edges
:%(NSLayoutConstraint%*)autoPinEdgeToSuperviewEdge:(ALEdge)edge%withInset:
(CGFloat)inset;%
‣

Pins the given edge of the view to the same edge of the superview with an
inset.

!
:%(NSLayoutConstraint%*)autoPinEdgeToSuperviewEdge:(ALEdge)edge%withInset:
(CGFloat)inset%relation:(NSLayoutRelation)relation;%
‣

Pins the given edge of the view to the same edge of the superview with an
inset as a maximum or minimum.

!
:%(NSArray%*)autoPinEdgesToSuperviewEdgesWithInsets:(UIEdgeInsets)insets;%
‣

Pins the edges of the view to the edges of its superview with the given edge
insets.
UIView+AutoLayout
Pin at point in superview
:%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%%
%%%%%%%%%%%%%%toPositionInSuperview:(CGFloat)value;%
‣

Pins the given edge of the view to a fixed position (X or Y value, depending on
edge) in the superview.
UIView+AutoLayout
Centering in superview
:%(NSArray%*)autoCenterInSuperview;%
‣

Centers the view in its superview.

!
:%(NSLayoutConstraint%*)autoCenterInSuperviewAlongAxis:(ALAxis)axis;%
‣

Centers the view along the given axis (horizontal or vertical) within its superview.

!
:%(NSLayoutConstraint%*)autoPinCenterAxis:(ALAxis)axis%
%% %
%
%
%%%toPositionInSuperview:(CGFloat)value;%
‣

Pins the given center axis of the view to a fixed position (X or Y value,
depending on axis) in the superview.
UIView+AutoLayout
Pin to other views
:%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge%
ofView:(UIView%*)peerView;%
‣

Pins an edge of the view to a given edge of another view.

!
:%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge%
ofView:(UIView%*)peerView%withOffset:(CGFloat)offset;%
‣

Pins an edge of the view to a given edge of another view with an offset.

!
:%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge%
ofView:(UIView%*)peerView%withOffset:(CGFloat)offset%relation:
(NSLayoutRelation)relation;%
‣

Pins an edge of the view to a given edge of another view with an offset as a
maximum or minimum.
UIView+AutoLayout
Aligning views
:%(NSLayoutConstraint%*)autoAlignAxis:(ALAxis)axis%%
%%%%%%%%%%%%%%%%%%%%%toSameAxisOfView:(UIView%*)peerView;%
‣

Aligns an axis of the view to the same axis of another view.

!
:%(NSLayoutConstraint%*)autoAlignAxis:(ALAxis)axis%%
%%%%%%%%%%%%%%%%%%%%%toSameAxisOfView:(UIView%*)peerView%%
%% %
%
%
%
%%%%%%withOffset:(CGFloat)offset;%
‣

Aligns an axis of the view to the same axis of another view with an offset.
UIView+AutoLayout
View dimensions
:%(NSArray%*)autoSetDimensionsToSize:(CGSize)size;%
‣

Sets the view to a specific size.

!
:%(NSLayoutConstraint%*)autoSetDimension:(ALDimension)dimension%%
%
%
%
%
%
%
%
%
%toSize:(CGFloat)size;%
‣

Sets the given dimension of the view to a specific size.

!
:%(NSLayoutConstraint%*)autoSetDimension:(ALDimension)dimension%%
%%% %
%
%
%
%
%
%
%toSize:(CGFloat)size%%
%
%
%
%
%
%
%
%%%relation:(NSLayoutRelation)relation;%
‣

Sets the given dimension of the view to a specific size as a maximum or
minimum.
UIView+AutoLayout
View dimension matching
:%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension:
(ALDimension)toDimension%ofView:(UIView%*)peerView;%
‣

Matches a dimension of the view to a given dimension of another view.

:%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension:
(ALDimension)toDimension%ofView:(UIView%*)peerView%withOffset:(CGFloat)offset;%
‣

Matches a dimension of the view to a given dimension of another view with an offset.

:%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension:
(ALDimension)toDimension%ofView:(UIView%*)peerView%withOffset:(CGFloat)offset%relation:
(NSLayoutRelation)relation;%
‣

Matches a dimension of the view to a given dimension of another view with an offset as a
maximum or minimum.

:%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension:
(ALDimension)toDimension%ofView:(UIView%*)peerView%withMultiplier:(CGFloat)multiplier;%
‣

Matches a dimension of the view to a multiple of a given dimension of another view.

:%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension:
(ALDimension)toDimension%ofView:(UIView%*)peerView%withMultiplier:(CGFloat)multiplier%
relation:(NSLayoutRelation)relation;%
‣

Matches a dimension of the view to a multiple of a given dimension of another view as a
maximum or minimum.
UIView+AutoLayout
Priorities
+%(void)autoSetPriority:(UILayoutPriority)priority%%
%%%%%%%%%forConstraints:(ALConstraintsBlock)block;%
‣

Sets the constraint priority to the given value for all constraints created using
the UIView+AutoLayout category API within the given constraints block.

‣

NOTE: This method will have no effect (and will NOT set the priority) on
constraints created or added using the SDK directly within the block!
‣

Use the auto… methods of UIView+AutoLayout
UIView+AutoLayout
Removal of constraints
:%(void)autoRemoveConstraintsAffectingView;%
‣

Removes all explicit constraints that affect the view.

:%(void)autoRemoveConstraintsAffectingViewAndSubviews;%
‣

Recursively removes all explicit constraints that affect the view and its subviews.

Additional%methods%if%you%do%want%to%remove%the%
:%(void)autoRemoveConstraintsAffectingViewIncludingImplicitConstraints:
(BOOL)shouldRemoveImplicitConstraints;%
‣

Removes all constraints that affect the view, optionally including implicit
constraints.

:
(void)autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstra
ints:(BOOL)shouldRemoveImplicitConstraints;%
‣

Recursively removes all constraints from the view and its subviews, optionally
including implicit constraints.
UIView+AutoLayout
Removal of constraints
‣

A little bit of a warning when removing constraints:
‣

The constraint solving engine is not optimised for
the removal of constraints
‣

‣
‣

Removing a lot of constraints can have serious
impact on performance
Use with care and in small quantities

You can change the priority of a constraint to “disable”
it (but you have to remember the original value to
reenable it)
UIView+AutoLayout
Other stuff
‣

category on NSArray doing the same for groups of
views

‣

category on NSLayoutConstraint to remove a
constraint from the view it's attached to
Protips
‣

Where to add which constraints?
‣

When constraints only apply to 1 view (eg setting
dimensions)
‣
‣

‣

create on view
add to view

When constraints apply to more than 1 view eg pin
one view to another
‣

create on target view

‣

add to common superview of both views (usually the
same)
Protips
‣

Check for autotranslated autoresizing constraints

‣

don’t forget translatesAutoresizingMaskIntoConstraints

‣

Display the view hierarchy from the root to find where
things go wrong
‣

errors don’t provide any context
!

‣

po%[[[[UIApplication%sharedApplication]%windows]%
objectAtIndex:0]%recursiveDescription]

look for address of view giving errors, and then
you’ll get an idea of what context the error is
happing in
Protips
‣

Don’t assume a view has a fixed size
‣

(unless you give it a size constraint)

‣

initWithFrame: not really useful anymore

‣

Don’t use frame/bounds calculations in constraints
‣

‣

exception: in layoutSubViews

Try to minimalise the number of constraints
‣

solving takes time

‣

more constraints, more complex
Protips
‣

animating constraints
‣

set constraints outside animation block

‣

call layoutIfNeeded in animation

‣

Make sure you do this on the correct “top level
view” otherwise nothing will animate!

self.offsetConstraint.constant%=%[self%calculateOffset];%
[UIView%animationWithDuration:0.3%animations:^{%
%
[self.view%layoutIfNeeded];%
}];
Protips
‣

when changing content:
‣

don’t forget to update constraints
‣

call setNeedsUpdateConstraints
‣

‣

or updateConstraintsIfNeeded
‣

‣

‣

let the system decide when to update

immediate action

only needed when your constraint change might affect
other constraints

don’t forget to relayout your views
‣

call setNeedsLayout

‣

or layoutIfNeeded
Useful References
‣ Apple’s autolayout documentation:
‣ reference: https://developer.apple.com/library/ios/
documentation/userexperience/conceptual/
AutolayoutPG/Introduction/Introduction.html
‣ Martin Pilkington’s iOSDevUK2013 slides on autolayout
solving
‣ http://www.iosdevuk.com/storage/talks2013/
MartinPilkington.pdf
‣ UIView+AutoLayout
‣ https://github.com/smileyborg/UIView-AutoLayout
Thanks for listening.
Questions? Contact me:
Twitter: @inferis
App.Net: @inferis
E-mail: tom@interfaceimplementation.be
vCard: http://inferis.org

Weitere ähnliche Inhalte

Andere mochten auch

iOSプログラミングでAutoLayoutを数式のように記述する
iOSプログラミングでAutoLayoutを数式のように記述するiOSプログラミングでAutoLayoutを数式のように記述する
iOSプログラミングでAutoLayoutを数式のように記述するHisakuni Fujimoto
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacMindfire Solutions
 
AutoLayout y Size Classes
AutoLayout y Size ClassesAutoLayout y Size Classes
AutoLayout y Size ClassesNSCoder Mexico
 
Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Giuseppe Arici
 
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)Derek Lee Boire
 

Andere mochten auch (6)

iOS AutoLayout
iOS AutoLayoutiOS AutoLayout
iOS AutoLayout
 
iOSプログラミングでAutoLayoutを数式のように記述する
iOSプログラミングでAutoLayoutを数式のように記述するiOSプログラミングでAutoLayoutを数式のように記述する
iOSプログラミングでAutoLayoutを数式のように記述する
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
AutoLayout y Size Classes
AutoLayout y Size ClassesAutoLayout y Size Classes
AutoLayout y Size Classes
 
Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013
 
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
Adjusting to Auto Layout (Tutorial / Tips for iOS Auto Layout)
 

Ähnlich wie Autolayout primer

Introduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKIntroduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKBrendan Lim
 
Vaadin DevDay 2017 - DI your UI
Vaadin DevDay 2017 - DI your UIVaadin DevDay 2017 - DI your UI
Vaadin DevDay 2017 - DI your UIPeter Lehto
 
Dynamic User Interfaces for Desktop and Mobile
Dynamic User Interfaces for Desktop and MobileDynamic User Interfaces for Desktop and Mobile
Dynamic User Interfaces for Desktop and Mobilepeychevi
 
ITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVCITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVCOrtus Solutions, Corp
 
Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Massimo Oliviero
 
Auto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSAuto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSLakhdeep Jawandha
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentanistar sung
 
Responsive layouts by Maqbool
Responsive layouts by MaqboolResponsive layouts by Maqbool
Responsive layouts by MaqboolNavin Agarwal
 
Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View ControllersBob McCune
 
Building An Accessible Site from the Ground Up
Building An Accessible Site from the Ground UpBuilding An Accessible Site from the Ground Up
Building An Accessible Site from the Ground UpRussell Heimlich
 
Turku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsTurku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsJames Stone
 
Fragments: Why, How, What For?
Fragments: Why, How, What For?Fragments: Why, How, What For?
Fragments: Why, How, What For?Brenda Cook
 
Testing android apps with espresso
Testing android apps with espressoTesting android apps with espresso
Testing android apps with espressoÉdipo Souza
 
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New ViewOdoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New ViewElínAnna Jónasdóttir
 
A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentKaty Slemon
 

Ähnlich wie Autolayout primer (20)

Introduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKIntroduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDK
 
Swift
SwiftSwift
Swift
 
Vaadin DevDay 2017 - DI your UI
Vaadin DevDay 2017 - DI your UIVaadin DevDay 2017 - DI your UI
Vaadin DevDay 2017 - DI your UI
 
Dynamic User Interfaces for Desktop and Mobile
Dynamic User Interfaces for Desktop and MobileDynamic User Interfaces for Desktop and Mobile
Dynamic User Interfaces for Desktop and Mobile
 
ITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVCITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVC
 
Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)
 
Android UI Development
Android UI DevelopmentAndroid UI Development
Android UI Development
 
GWT widget development
GWT widget developmentGWT widget development
GWT widget development
 
Auto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSAuto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOS
 
04 objective-c session 4
04  objective-c session 404  objective-c session 4
04 objective-c session 4
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app development
 
Responsive layouts by Maqbool
Responsive layouts by MaqboolResponsive layouts by Maqbool
Responsive layouts by Maqbool
 
Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View Controllers
 
Building An Accessible Site from the Ground Up
Building An Accessible Site from the Ground UpBuilding An Accessible Site from the Ground Up
Building An Accessible Site from the Ground Up
 
Turku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsTurku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-components
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Fragments: Why, How, What For?
Fragments: Why, How, What For?Fragments: Why, How, What For?
Fragments: Why, How, What For?
 
Testing android apps with espresso
Testing android apps with espressoTesting android apps with espresso
Testing android apps with espresso
 
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New ViewOdoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
 
A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter component
 

Mehr von Inferis

Practical auto layout
Practical auto layoutPractical auto layout
Practical auto layoutInferis
 
Communicating with GIFs
Communicating with GIFsCommunicating with GIFs
Communicating with GIFsInferis
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtimeInferis
 
Adventures in Multithreaded Core Data
Adventures in Multithreaded Core DataAdventures in Multithreaded Core Data
Adventures in Multithreaded Core DataInferis
 
I Am A Switcher
I Am A SwitcherI Am A Switcher
I Am A SwitcherInferis
 
Dynamic Silverlight
Dynamic SilverlightDynamic Silverlight
Dynamic SilverlightInferis
 

Mehr von Inferis (6)

Practical auto layout
Practical auto layoutPractical auto layout
Practical auto layout
 
Communicating with GIFs
Communicating with GIFsCommunicating with GIFs
Communicating with GIFs
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
 
Adventures in Multithreaded Core Data
Adventures in Multithreaded Core DataAdventures in Multithreaded Core Data
Adventures in Multithreaded Core Data
 
I Am A Switcher
I Am A SwitcherI Am A Switcher
I Am A Switcher
 
Dynamic Silverlight
Dynamic SilverlightDynamic Silverlight
Dynamic Silverlight
 

Kürzlich hochgeladen

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 

Kürzlich hochgeladen (20)

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Autolayout primer

  • 4. I love... ‣ ... my wife ‣ ... my 4 kids ‣ ... to code ‣ ... to play a game of squash ‣ ... good beer
  • 5. I open sourced... ... some code: ‣ IIViewDeckController: “An implementation of the sliding functionality found in the Path 2.0 or Facebook iOS apps.” ‣ IIDateExtensions ‣ IIPopoverStatusItem See: http://github.com/inferis
  • 6. I made... ... some apps: Butane http://getbutane.com Hi, @10to1! Drash http://dra.sh
  • 8. Agenda ‣ What is Autolayout? ‣ What is a constraint? ‣ Autolayout in Xcode designer ‣ Autolayout in code
  • 10. What is Autolayout? According to the Apple Docs: Auto%Layout%is%a%system%that%lets%you%lay%out%your%app’s%user%interface%by% creating%a%mathematical%description%of%the%relationships%between%the% elements.%% ‣ Keywords: ‣ ‣ ‣ mathematical relationships Eh? ‣ Developer defines constraints on/between elements ‣ constraints translate into mathematical functions ‣ UIKit/Appkit tries to solve these functions
  • 11. What is Autolayout? ‣ Correctly using autolayout requires you to change thinking ‣ Autolayout works in a different way than what you were used to ‣ Don't go calculating frames anymore ‣ Define how an element should behave according to it's peers: ‣ parent ‣ siblings ‣ Don't think in absolute terms, but think in relational terms ‣ Think flexible: it's not because an element has a certain size at one point, it will have the same size at another point
  • 13. Constraint a%mathematical%representation%of%a%human:expressable%statement% "the left edge should be 20pt of the superviews left edge" view.left = (superview.left + 20) y=m*x+c (y%=%attribute1%=%view.left)% (x%=%attribute2%=%superview.left)% (m%=%multiplier%=%1)% (c%=%constant%=%20)
  • 14. Constraint ‣ two items with an attribute ‣ constant ‣ relation ‣ multiplier ‣ priority level //%firstItem.firstAttribute%{==,<=,>=}%secondItem.secondAttribute%*%multiplier%+%constant% ! @property%(readonly,%assign)%id%firstItem;% @property%(readonly)%NSLayoutAttribute%firstAttribute;% @property%(readonly)%NSLayoutRelation%relation;% @property%(readonly,%assign)%id%secondItem;% @property%(readonly)%NSLayoutAttribute%secondAttribute;% @property%(readonly)%CGFloat%multiplier;% ! @property%CGFloat%constant;
  • 15. Attribute? ‣ left ‣ right ‣ top ‣ bottom ‣ leading ~ left or right depending on language settings ‣ trailing ~ right or left depending on language settings ‣ width ‣ height ‣ centerX ‣ centerY ‣ baseline
  • 17. Multiplier? ‣ Apply modifier to attribute of source item ‣ Cannot be set using the designer ‣ Can be set in code
  • 18. Relation? ‣ equal ‣ less than or equal ‣ greater than or equal ! ‣ if you want just "less than" or "greater than", adjust the constant
  • 19. Priority ‣ constraints with higher priority are satisfied before constraints with lower priority ‣ Default = UILayoutPriorityRequired (1000) ‣ Other predefined priorities ‣ UILayoutPriorityRequired = 1000 ‣ UILayoutPriorityDefaultHigh = 750 ‣ ‣ UILayoutPriorityDefaultLow = 250 ‣ ‣ ‣ Default content compression priority Default content hugging priority UILayoutPriorityFittingSizeLevel = 50 You can use any integer value between 0-1000
  • 20. Pitfalls ‣ constraints are cumulative ‣ constraints do not override each other ‣ ‣ ‣ adding a second width constraint does not remove or override a previous one remove first one manually constraints can cross view hierarchy ‣ add constraint from view to superview of superview ‣ only if scope of view hierarchy uses autolayout! (no custom framesetting)
  • 21. Intrisic Content Size ‣ applicable to leaf views (views with no subviews) ‣ view knows what size it wants to be (eg UIButton) ‣ autolayout will size view to its intristic content size if no constraints control the width/height :%(CGSize)intrinsicContentSize; Returns%the%natural%size%for%the%receiving%view,%considering% only%properties%of%the%view%itself.% ‣ You can override this method in a subclass to change the default behavior ‣ return UIViewNoIntrinsicMetric when there's no intristic size defined for an axis
  • 22. Compression resistance & Content hugging ‣ Content Compression resistance? ‣ ‣ defines how the view's frame resist compressing it's content Content hugging? ‣ defines how the view's frame should hug it's content
  • 23. Compression resistance & Content hugging Say you've got button like this: % |[%%%%%%%Click%Me%%%%%%]|% and you've pinned the edges to a larger superview with priority 500. ! Then, if hugging priority > 500 it'll look like this: % |[Click%Me]%%%%%%%%%%%%%|% If hugging priority < 500 it'll look like this: % |[%%%%%%%Click%Me%%%%%%]|% ! If the superview now shrinks and if the compression resistance priority > 500, it'll look like this % |[Click%|Me]% else if compression resistance priority < 500, it could look like this: % |[Cli..]|
  • 25. Demo
  • 27. Autolayout in code: 3 options ‣ Write constraints manually ‣ ‣ Use visual format ‣ ‣ HEAD WILL EXPLODE HEAD WILL EXPLODE MOAR Use UIView+AutoLayout Cocoapod ‣ Provides descriptive methods creating constraints ‣ Still verbose but quite readable ‣ Best of both worlds
  • 28. Autolayout in code ‣ Important note when creating constraints in code ‣ If you create a view manually, use: ! view%=%[UIView%new];% view.translatesAutoresizingMaskIntoConstraints%=%NO; ! ‣ if you don’t, you’ll get a lot of constraint errors!
  • 29. UIView+AutoLayout ‣ Available as a CocoaPod ‣ Implements a category on UIView. ‣ Easy creation of 1 or more NSLayoutConstraints. ‣ Some methods return 1 constraint, some an NSArray containing constraints ‣ Does not add constraints, just creates them ‣ you need to add them yourself :(void)addConstraint:(NSLayoutConstraint%*)constraint;% :(void)addConstraints:(NSArray%*)constraints;
  • 30. UIView+AutoLayout Creating an autolayouted view ‣ Don't use: translatesAutoresizingMaskIntoConstraints ! % % % view%=%[UIView%newAutoLayoutView];% //%or% view%=%[[UIView%alloc]%initForAutoLayout];
  • 31. UIView+AutoLayout Pin to superview edges :%(NSLayoutConstraint%*)autoPinEdgeToSuperviewEdge:(ALEdge)edge%withInset: (CGFloat)inset;% ‣ Pins the given edge of the view to the same edge of the superview with an inset. ! :%(NSLayoutConstraint%*)autoPinEdgeToSuperviewEdge:(ALEdge)edge%withInset: (CGFloat)inset%relation:(NSLayoutRelation)relation;% ‣ Pins the given edge of the view to the same edge of the superview with an inset as a maximum or minimum. ! :%(NSArray%*)autoPinEdgesToSuperviewEdgesWithInsets:(UIEdgeInsets)insets;% ‣ Pins the edges of the view to the edges of its superview with the given edge insets.
  • 32. UIView+AutoLayout Pin at point in superview :%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%% %%%%%%%%%%%%%%toPositionInSuperview:(CGFloat)value;% ‣ Pins the given edge of the view to a fixed position (X or Y value, depending on edge) in the superview.
  • 33. UIView+AutoLayout Centering in superview :%(NSArray%*)autoCenterInSuperview;% ‣ Centers the view in its superview. ! :%(NSLayoutConstraint%*)autoCenterInSuperviewAlongAxis:(ALAxis)axis;% ‣ Centers the view along the given axis (horizontal or vertical) within its superview. ! :%(NSLayoutConstraint%*)autoPinCenterAxis:(ALAxis)axis% %% % % % %%%toPositionInSuperview:(CGFloat)value;% ‣ Pins the given center axis of the view to a fixed position (X or Y value, depending on axis) in the superview.
  • 34. UIView+AutoLayout Pin to other views :%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge% ofView:(UIView%*)peerView;% ‣ Pins an edge of the view to a given edge of another view. ! :%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge% ofView:(UIView%*)peerView%withOffset:(CGFloat)offset;% ‣ Pins an edge of the view to a given edge of another view with an offset. ! :%(NSLayoutConstraint%*)autoPinEdge:(ALEdge)edge%toEdge:(ALEdge)toEdge% ofView:(UIView%*)peerView%withOffset:(CGFloat)offset%relation: (NSLayoutRelation)relation;% ‣ Pins an edge of the view to a given edge of another view with an offset as a maximum or minimum.
  • 35. UIView+AutoLayout Aligning views :%(NSLayoutConstraint%*)autoAlignAxis:(ALAxis)axis%% %%%%%%%%%%%%%%%%%%%%%toSameAxisOfView:(UIView%*)peerView;% ‣ Aligns an axis of the view to the same axis of another view. ! :%(NSLayoutConstraint%*)autoAlignAxis:(ALAxis)axis%% %%%%%%%%%%%%%%%%%%%%%toSameAxisOfView:(UIView%*)peerView%% %% % % % % %%%%%%withOffset:(CGFloat)offset;% ‣ Aligns an axis of the view to the same axis of another view with an offset.
  • 36. UIView+AutoLayout View dimensions :%(NSArray%*)autoSetDimensionsToSize:(CGSize)size;% ‣ Sets the view to a specific size. ! :%(NSLayoutConstraint%*)autoSetDimension:(ALDimension)dimension%% % % % % % % % % %toSize:(CGFloat)size;% ‣ Sets the given dimension of the view to a specific size. ! :%(NSLayoutConstraint%*)autoSetDimension:(ALDimension)dimension%% %%% % % % % % % % %toSize:(CGFloat)size%% % % % % % % % %%%relation:(NSLayoutRelation)relation;% ‣ Sets the given dimension of the view to a specific size as a maximum or minimum.
  • 37. UIView+AutoLayout View dimension matching :%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension: (ALDimension)toDimension%ofView:(UIView%*)peerView;% ‣ Matches a dimension of the view to a given dimension of another view. :%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension: (ALDimension)toDimension%ofView:(UIView%*)peerView%withOffset:(CGFloat)offset;% ‣ Matches a dimension of the view to a given dimension of another view with an offset. :%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension: (ALDimension)toDimension%ofView:(UIView%*)peerView%withOffset:(CGFloat)offset%relation: (NSLayoutRelation)relation;% ‣ Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum. :%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension: (ALDimension)toDimension%ofView:(UIView%*)peerView%withMultiplier:(CGFloat)multiplier;% ‣ Matches a dimension of the view to a multiple of a given dimension of another view. :%(NSLayoutConstraint%*)autoMatchDimension:(ALDimension)dimension%toDimension: (ALDimension)toDimension%ofView:(UIView%*)peerView%withMultiplier:(CGFloat)multiplier% relation:(NSLayoutRelation)relation;% ‣ Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum.
  • 38. UIView+AutoLayout Priorities +%(void)autoSetPriority:(UILayoutPriority)priority%% %%%%%%%%%forConstraints:(ALConstraintsBlock)block;% ‣ Sets the constraint priority to the given value for all constraints created using the UIView+AutoLayout category API within the given constraints block. ‣ NOTE: This method will have no effect (and will NOT set the priority) on constraints created or added using the SDK directly within the block! ‣ Use the auto… methods of UIView+AutoLayout
  • 39. UIView+AutoLayout Removal of constraints :%(void)autoRemoveConstraintsAffectingView;% ‣ Removes all explicit constraints that affect the view. :%(void)autoRemoveConstraintsAffectingViewAndSubviews;% ‣ Recursively removes all explicit constraints that affect the view and its subviews. Additional%methods%if%you%do%want%to%remove%the% :%(void)autoRemoveConstraintsAffectingViewIncludingImplicitConstraints: (BOOL)shouldRemoveImplicitConstraints;% ‣ Removes all constraints that affect the view, optionally including implicit constraints. : (void)autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstra ints:(BOOL)shouldRemoveImplicitConstraints;% ‣ Recursively removes all constraints from the view and its subviews, optionally including implicit constraints.
  • 40. UIView+AutoLayout Removal of constraints ‣ A little bit of a warning when removing constraints: ‣ The constraint solving engine is not optimised for the removal of constraints ‣ ‣ ‣ Removing a lot of constraints can have serious impact on performance Use with care and in small quantities You can change the priority of a constraint to “disable” it (but you have to remember the original value to reenable it)
  • 41. UIView+AutoLayout Other stuff ‣ category on NSArray doing the same for groups of views ‣ category on NSLayoutConstraint to remove a constraint from the view it's attached to
  • 42. Protips ‣ Where to add which constraints? ‣ When constraints only apply to 1 view (eg setting dimensions) ‣ ‣ ‣ create on view add to view When constraints apply to more than 1 view eg pin one view to another ‣ create on target view ‣ add to common superview of both views (usually the same)
  • 43. Protips ‣ Check for autotranslated autoresizing constraints ‣ don’t forget translatesAutoresizingMaskIntoConstraints ‣ Display the view hierarchy from the root to find where things go wrong ‣ errors don’t provide any context ! ‣ po%[[[[UIApplication%sharedApplication]%windows]% objectAtIndex:0]%recursiveDescription] look for address of view giving errors, and then you’ll get an idea of what context the error is happing in
  • 44. Protips ‣ Don’t assume a view has a fixed size ‣ (unless you give it a size constraint) ‣ initWithFrame: not really useful anymore ‣ Don’t use frame/bounds calculations in constraints ‣ ‣ exception: in layoutSubViews Try to minimalise the number of constraints ‣ solving takes time ‣ more constraints, more complex
  • 45. Protips ‣ animating constraints ‣ set constraints outside animation block ‣ call layoutIfNeeded in animation ‣ Make sure you do this on the correct “top level view” otherwise nothing will animate! self.offsetConstraint.constant%=%[self%calculateOffset];% [UIView%animationWithDuration:0.3%animations:^{% % [self.view%layoutIfNeeded];% }];
  • 46. Protips ‣ when changing content: ‣ don’t forget to update constraints ‣ call setNeedsUpdateConstraints ‣ ‣ or updateConstraintsIfNeeded ‣ ‣ ‣ let the system decide when to update immediate action only needed when your constraint change might affect other constraints don’t forget to relayout your views ‣ call setNeedsLayout ‣ or layoutIfNeeded
  • 47. Useful References ‣ Apple’s autolayout documentation: ‣ reference: https://developer.apple.com/library/ios/ documentation/userexperience/conceptual/ AutolayoutPG/Introduction/Introduction.html ‣ Martin Pilkington’s iOSDevUK2013 slides on autolayout solving ‣ http://www.iosdevuk.com/storage/talks2013/ MartinPilkington.pdf ‣ UIView+AutoLayout ‣ https://github.com/smileyborg/UIView-AutoLayout
  • 48. Thanks for listening. Questions? Contact me: Twitter: @inferis App.Net: @inferis E-mail: tom@interfaceimplementation.be vCard: http://inferis.org