AUTO LAYOUT “Fun” with Apple’s new layout system

I’M FLIP @flipsasser https://github.com/flipsasser [email protected]

https://github.com/BackForty/actually_invented_here

WTF IS AUTO LAYOUT? It’s a significant upgrade to the “springs and struts” layout system

BEFORE: SPRINGS & STRUTS ts!

I

stru & s g n i r p ix s n u ’s t I . . . know this

ONE CHECKBOX LATER AND...

“WUT”

FIRST: CONTENT HUGGING PRIORITY How close a parent’s bounds get to the child Greater than 500: bounds stand their ground Less than 500: bounds expand with parent

SECOND: CONTENT COMPRESSION RESISTANCE PRIORITY How much a child pushes back on parent’s bounds Same value rules as content

THIRD: CONSTRAINTS Define absolute or relative margins and positioning based on siblings or parent views

THUS, ALL CONTENT INTELLIGENTLY RESIZES ACCORDING TO RULES Caveat: this rarely looks “intelligent”

IT’S BASED ON “INTRINSIC SIZE” Or, “how would something look if there were no constraints applied?”

AUTO LAYOUT IN THE UI ... is for suckers

gs n i l b i s o t plied p a t n e r a np o s t n i a r t s Con

THIS IS WHY AUTO LAYOUT IS SO FRUSTRATING TO BEGINNERS

BOTTOM LINE:

Don’t do it. It sucks.

AUTO LAYOUT IN CODE ... is for happy developers Well, happy-ish

THE WEIRD WAY constraintsWithVisualFormat: uses an NSPredicate-style “language”

|[VIEW]-|

Flush to the left edge of superview; Apple UI standard space to the right

V:[SIBLING]-10-[VIEW(100)]

10 pixels down from siblings; exactly 100 pixels wide

[VIEW(==SIBLING)]

The same width as sibling

V:[VIEW(==SIBLING)]

The same height as sibling

BUT HOW DOES IT KNOW WHAT VIEW AND SIBLING ARE?

THE MOST AMAZING METHOD EVER

NSDictionaryOfVariableBindings

BEFORE YOU KNEW ABOUT NSDICTIONARYOFVARIABLEBINDINGS

NSValue *someValue = [MyClass valueWithThingAndAThing:@"Yup"]; NSDictionary *myNeatDict = [NSDictionary dictionaryWithObjectsAndKeys:someValue, @"someValue", nil];

AFTER YOU KNEW ABOUT NSDICTIONARYOFVARIABLEBINDINGS

NSValue *someValue = [MyClass valueWithThingAndAThing:@"Yup"]; NSDictionary *myNeatDict = NSDictionaryOfVariableBindings(someValue);

BOTH PRODUCE THE SAME THING: {“someValue” => [NSValue instance]}

USED WITH CONSTRAINTS NSView *sibling = [self.superview.subviews objectAtIndex:1]; NSDictionary *views = NSDictionaryOfVariableBindings(sibling); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[sibling]-|" options:nil metrics:nil views:views]; *tell the sibling view to keep the uniform distance to the left and right of the parent

NOW APPLY THE ARRAY OF CONSTRAINTS

[self.superview addConstraints:constraints];

THE NORMAL WAY constraintWithItem, because you program for a living

GET A SINGLE CONSTRAINT

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];

AMAZING AWESOMENESS OF THIS APPROACH Applies directly to related views Specifies relationship granularly Multipliers (e.g. “1.5 x sibling”) Constants (“always 100px”) You don’t forget to instantiate an array because it’s just an NSLayoutConstraint

THE HOLY GRAIL StackableView

STACKS VIEWS OF RANDOM HEIGHTS BELOW EACH OTHER

BOOM!

HOW IT WORKS addSubview creates constraints: Top constrained to the previous child view’s bottom Left and right constrained to parent view’s left and right Bottom constrained to the next child’s top or the parent view’s bottom

GOTCHAS

NESTING IS A DISASTER A view whose intrinsic size is the sum of its children’s intrinsic sizes is a disaster waiting to happen

DEBUGGING IS KEY Edit Scheme -> Run -> Arguments -> -NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints YES

TURNS THIS...

INTO THIS!

DRAWBACKS It ain’t intuitive It can be tricky to debug Also...

YOU MAY HAVE TO DO STUFF LIKE THIS

// Remove bottom layout constraints from the document view for (NSLayoutConstraint *constraint in self.view.superview.constraints) { if (constraint.secondItem == self.view && constraint.firstAttribute == NSLayoutAttributeBottom) { [self.view.superview removeConstraint:constraint]; NSLayoutConstraint *saneConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view.superview attribute:NSLayoutAttributeBottom multiplier:0.0 constant:20.0]; [self.view.superview addConstraint:saneConstraint]; } }

FEED ME YOUR QUESTIONS I AM A QUESTION MONSTER AND I NEED THEM TO SURVIVE

THNAKS!

“Fun” with Apple's new layout system - GitHub

AUTO LAYOUT. “Fun” with Apple's new layout system ... Flush to the left edge of superview; Apple UI standard space to the right. |[VIEW]-| ...

2MB Sizes 5 Downloads 53 Views

Recommend Documents

LED-mirror layout - GitHub
We defined all pixelsstrips in the screen as one big row (MAX config during startup). This all means every byte of pixel data controls 4 leds at a time. And MAX ...

routine management system - GitHub
10. Figure 4 - Sample Data Set of Routine Management System . .... platform apps, conventional software architectural design patterns may be adopted and ...

System Requirements Specification - GitHub
This section describes the scope of Project Odin, as well as an overview of the contents of the SRS doc- ument. ... .1 Purpose. The purpose of this document is to provide a thorough description of the requirements for Project Odin. .... Variables. â€

System Requirements Specification - GitHub
System Requirements Specification. Project Odin. Kyle Erwin. Joshua Cilliers. Jason van Hattum. Dimpho Mahoko. Keegan Ferrett. Note: This document is constantly under revision due to our chosen methodology, ... This section describes the scope of Pro

FreeBSD ports system - GitHub
Search - make search (cont'd). Port: rsync-3.0.9_3. Path: /usr/ports/net/rsync. Info: Network file distribution/synchronization utility. Maint: [email protected].

CodaLab Worker System - GitHub
The worker system consists of 3 components: • REST server: ... a ”check out” call which is used to tell the server that a worker is shutting down and prevent it from.

CBIR System - GitHub
Final result was a Matlab built software application, with an image database, that utilized ... The main idea is to integrate the strengths of content- and keyword-based image ..... In the following we present some of the best search results.

Deploying Custom Microsoft Office Templates with System ... - GitHub
... MANAGER. Page 2 of 25. Author. Trevor Jones. Version. 1.0. Date. 2017-03-07. Blog smsagent.wordpress.com ..... 24. Updating a Template .

A Simple Visual Navigation System with Convergence ... - GitHub
University of Lincoln ... Czech Technical University in Prague. {tkrajnik ... saves its descriptor, image coordinates and robot distance from segment start. ..... Research program funded by the Ministry of Education of the Czech Republic. No.

DFKI System Combination with Sentence Ranking at ... - GitHub
data provided as part of the shared task. The proposed ... the meta-data from the two systems themselves. .... tence selection themselves, big sets increase the.

Polyglot Build System With Build Artifact Repository - GitHub
Maven Repository: The build artifact repository layout introduced in Maven 2. ... (Details at: http://ant.apache.org/ivy/history/latest-milestone/resolver/url.html).

Fun with Functions Accounts
Microsoft Excel: Fun with Functions. J. Dee Itri, Excel Maze. ○ Cell referencing. ○ Row numbers and column letters (E7, G4, BZ12). ○ =5+5 vs =A1+A2. ○ Ranges (A:A, 1:4, A5:B14). ○ The great and powerful “$” sign. ○ Relative (B5). ○

Open Vehicle Monitoring System - GitHub
Aug 14, 2013 - 10. CONFIGURE THE GPRS DATA CONNECTION (NEEDED FOR ...... Using the OVMS smartphone App (Android or Apple iOS), set Feature ...

Historical Query/Response System - GitHub
Feb 12, 2010 - developer website. Tick Query Examples. In order to query all the ticks for Google between 9 am and 12 pm on February 3, 2009, execute:.

Open Vehicle Monitoring System - GitHub
Feb 5, 2017 - GITHUB. 10. COMPILE AND FLASH YOUR FIRST FIRMWARE. 10. CHIPS USED .... If your laptop already has a RS232 port, then you can ... download your own forked repository from github to your local computer. Detailed ...

The Dissident File System - GitHub
Preferably compressed data like media files. Cryptographically secure ... Analysis of Adversary and Threats. Some attack ... Store sensitive data in free space?

New PDF File - GitHub
Waibhav Yadavdev. [f] /Vaiyadav. /Dev Vaibhav Yadav. [in] /devvaibhavyadav. º & Dev Vaibhav. Professional Skills. Software Skills. IntelliJ IDEA. Android Studio.

New Output - GitHub
Sep 12, 2015 - USB-FIFO Interface (Up to 10 MB/s transfer speed). 64 Mbit .... net class. ClassName: FT_JTAG. FPGA_CLK. 22R. RA2D. 22R. RA2B. 22R.

New Output - GitHub
Aug 29, 2016 - SPI 0 FS0LS. SPI 0 FS1LS. SPI 0 FS2LS. SPI 0 CLKLS. SPI 0 MOSILS. SPI 0 MISOLS. I2C 1 SDALS. I2C 1 SCLLS. I2C 2 SDALS. I2C 2 SCLLS.