SlideShare uma empresa Scribd logo
1 de 49
Baixar para ler offline
...ifthere'sonethingiOScommunitydoes
notlackareattemptstorewriteoreven
entirelyreplaceAutoLayout....
IconsiderAutoLayoutoneofthetop5,
maybetop3technologiestoevercomeout
ofApple.It'sbeautiful,easytoworkwith
andthere'snothingIcan'tdowithit.It's
Apple'schoiceforUIlayoutandyoushould
masterit,notavoidit.
—Aleksandar Vacić,http://aplus.rs/2017/much-ado-about-ios-app-
architecture/
...
UIKitAutoLayoutandInterfaceBuilderare
notsupportedbyTexture[anopensource
project].Itisworthnotingthatbothofthese
technologiesarenotpermittedinestablished
anddisciplinediOSdevelopmentteams,such
asatFacebook,Instagram,andPinterest.
—http://texturegroup.org/docs/faq.html#texture-does-not-support-uikit-
auto-layout-or-interfacebuilder
...
Ishavedthousandsoflinesofboilerplate
byleveragingAutoLayout.Many
developersstillinsistonmanuallayout.
Maybetheydon'tunderstandAuto-
Layout,ormaybetheyheardastoryfrom
afriendofafriendabouthowAuto-Layout
isslow.(Itisn't.)
I'veseentoomanydevelopers—especially
atlargecompanies—inventin-house
layoutengine.That'sjustnuts.Don'tbloat
yourappwithabespokeframeworkwhen
Applebundlesafinelayoutenginewith
theOS.
—Ben Sandofsky,https://blog.halide.cam/one-weird-trick-to-lose-size-
c0a4013de331
...
...SinceiOS8,wecanuseautomatic
heightcalculationwithoutimplementing
mentionedmethodatthedelegateof
UITableView.Toachievethisyoumayuse
AutoLayouttoolandrowHeightvariable
settoUITableViewAutomaticDimension...
Despitetheabilitytousethesetools,I
stronglyrecommendyoudonot.Andmore 
— Irecommendnotusingevencomplex
mathcalculationsonyourwaytodefine
furtherheightofacell,onlyaddition,
subtraction,multiplicationanddivision(if
possible).
ButwhataboutAutoLayout?Isitreallysoslowas
Iwastalkingabout?Probablyyouwillbe
surprisedbutit'struth.Incrediblyslowifyouwant
seeperfectsmoothscrollinginyourapponall
actualdeviceswhichhavelonglifecycle(especially
comparedtoAndroiddevices).Andmoresubviews
youhave,lessquicklyAutoLayoutworks.
—Alexander Orlov,https://medium.com/ios-os-x-development/perfect-smooth-
scrolling-in-uitableviews-fd609d5275a5
Manual Layout
Revisited
Everyone needs to handle layout.
Apps are different.
Non-engineering factors also matter.
view1.frame = CGRect(x: 0, y: 100, height: 32, width: 320)
view2.frame.origin.x = 0
view2.frame.origin.y = view1.frame.origin.y + view1.frame.size.height + 10
view2.frame.size.width = 100
view2.frame.size.height = view.frame.size.height - view1.frame.size.height - 10
view3.frame.origin.x = view2.center - view2.frame.width / 4
view3.frame.origin.y = view2.frame.origin.y
view3.frame.size.width = view2.frame.width / 2
view3.frame.size.height = view2.frame.origin.y
view4.frame.origin.x = view3.center - view3.frame.width / 4
view4.frame.origin.y = view3.frame.origin.y
view4.frame.size.width = view3.frame.width / 2
view4.frame.size.height = view3.frame.origin.y
Auto layout made me reconsider how
I write manual layout.
Manual layout operations can be
easily consolidated into simple
functions.
Manual Layout Operations
— Add convenience methods to simplify code
Manual Layout Operations
— Add convenience methods to simplify code
— Apply changes to collections of views at the same time
Manual Layout Operations
— Add convenience methods to simplify code
— Apply changes to collections of views at the same time
— Arrange a collection of views in a particular order
Manual Layout Operations
— Add convenience methods to simplify code
— Apply changes to collections of views at the same time
— Arrange a collection of views in a particular order
— Resize one view to make a collection fit on the screen
Creating Convenience Methods
These are extensions that simplify reasoning about layout code.
Creating Convenience Methods
extension UIView {
var bottom: CGFloat {
get {
return frame.maxY
}
set(newValue) {
frame.origin.y = newValue - frame.height
}
}
}
// ...
myView.bottom = 100
Creating Convenience Methods
extension UIView {
var bottomRightCorner: CGPoint {
get {
return CGPoint(x: frame.maxX, y: frame.maxY)
}
set(newValue) {
frame.origin = CGPoint(
x: newValue.x - frame.size.width,
y: newValue.y - frame.size.height
)
}
}
}
Demo
Creating Operations on Collections of Views
Applying the methods above to an array of views is a useful way to reduce
code.
Creating Operations on Collections of Views
func resize(views: [UIView], withWidth width: CGFloat) {
views.forEach { view in
view.width = width
}
}
Creating Operations on Collections of Views
func reposition(views: [UIView], withLeft left: CGFloat) {
views.forEach { view in
view.left = left
}
}
Demo
Arranging Horizontally or Vertically
It is common to arrange views along an axis (consider UIStackView).
Arranging Horizontally or Vertically
func arrangeTopToBottom(views: [UIView]) {
guard var currentPosition = views.first?.top else { return }
for view in views {
view.top = currentPosition
currentPosition += view.height
}
}
Resizing to Fit On Screen
Most of the time,a layout design will fit into two cases:
— The layout is not intended to fit on the screen.Usually this type of
layout will be placed inside a UIScrollView or one of its subclasses.
— The layout is intended to fit on the screen.
Resizing to Fit On Screen
For the second case—in a surprising number of instances—the layout can
be achieved by simply resizing a single view so that its height fills
whatever space is left by the other views.
Resizing to Fit On Screen
func resize(view: UIView, in views: [UIView], toFitHeight height: CGFloat) {
let heightOfViews = views.reduce(CGFloat(0)) { memo, currentView in
guard view != currentView else { return memo }
var mutableMemo = memo
mutableMemo += view.height
return mutableMemo
}
view.height = height - heightOfViews
}
Resizing to Fit On Screen
func resize(views:[UIView], toFitHeight height: CGFloat) {
views.forEach { view in
view.height = height / CGFloat(views.count)
}
}
Resizing to Fit On Screen
— There are a lot of potential resizing functions.
— Resizing also requires considering potential error cases.Can you
guarantee that the content will fit?
Demo
What about...?
— Top or bottom margins
What about...?
— Top or bottom margins
— Spacing between the elements
What about...?
— Top or bottom margins
— Spacing between the elements
— Sizing UILabels or UITextViews to fit their content
What about...?
— Top or bottom margins
— Spacing between the elements
— Sizing UILabels or UITextViews to fit their content
— Aligning to the first or last baseline text
What about...?
— Top or bottom margins
— Spacing between the elements
— Sizing UILabels or UITextViews to fit their content
— Aligning to the first or last baseline text
— Sizing UIImageViews to preserve their aspect ratio
What about...?
— Top or bottom margins
— Spacing between the elements
— Sizing UILabels or UITextViews to fit their content
— Aligning to the first or last baseline text
— Sizing UIImageViews to preserve their aspect ratio
What about...?
— Top or bottom margins
— Spacing between the elements
— Sizing UILabels or UITextViews to fit their content
— Aligning to the first or last baseline text
— Sizing UIImageViews to preserve their aspect ratio
— Leading and trailing rather than left and right
These methods are just an
introduction.

Mais conteúdo relacionado

Semelhante a Manual Layout Revisited

03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
Wingston
 

Semelhante a Manual Layout Revisited (20)

Programming with JavaFX
Programming with JavaFXProgramming with JavaFX
Programming with JavaFX
 
The EffiChange XPager Suite: Understanding XPages Scaffolding
The EffiChange XPager Suite: Understanding XPages ScaffoldingThe EffiChange XPager Suite: Understanding XPages Scaffolding
The EffiChange XPager Suite: Understanding XPages Scaffolding
 
User experience and interactions design
User experience and interactions design User experience and interactions design
User experience and interactions design
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIView
 
Performance #3 layout&animation
Performance #3  layout&animationPerformance #3  layout&animation
Performance #3 layout&animation
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 
iOS Development (Part 2)
iOS Development (Part 2)iOS Development (Part 2)
iOS Development (Part 2)
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack Compose
 
mobl
moblmobl
mobl
 
Programming with JavaFX
Programming with JavaFXProgramming with JavaFX
Programming with JavaFX
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
 
How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in Scilab
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)
 
Google Associate Android Developer Certification
Google Associate Android Developer CertificationGoogle Associate Android Developer Certification
Google Associate Android Developer Certification
 
Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5Developing AIR for Android with Flash Professional CS5
Developing AIR for Android with Flash Professional CS5
 
Flutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdfFlutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdf
 
Compose In Practice
Compose In PracticeCompose In Practice
Compose In Practice
 
How Create an Animated Web Banner with InDesign & in5
How Create an Animated Web Banner with InDesign & in5How Create an Animated Web Banner with InDesign & in5
How Create an Animated Web Banner with InDesign & in5
 
Android UI Development
Android UI DevelopmentAndroid UI Development
Android UI Development
 

Mais de gillygize (7)

Some Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and typesSome Stuff I was thinking about state machines and types
Some Stuff I was thinking about state machines and types
 
Optimize llvm
Optimize llvmOptimize llvm
Optimize llvm
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopment
 
ViewController/State
ViewController/StateViewController/State
ViewController/State
 
Two-StageCreation
Two-StageCreationTwo-StageCreation
Two-StageCreation
 
Categories
CategoriesCategories
Categories
 

Último

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 

Manual Layout Revisited