Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Layout without Auto Layout
Search
Elvis Shi
May 23, 2017
Programming
450
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Layout without Auto Layout
Elvis Shi
May 23, 2017
More Decks by Elvis Shi
See All by Elvis Shi
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
580
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
570
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
160
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
410
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
790
How did I build an Open-Source SwiftUI Toast Library
lovee
1
190
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
1.3k
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
390
Observation のあれこれ / A brief introduction about Observation
lovee
3
460
Other Decks in Programming
See All in Programming
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.7k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
300
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
カツオ、ご期待ください
suneo3476
0
190
Next.js: The final boss of Web Performance
guaca
0
250
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
120
Kotlinの15年とServer-Side Kotlin
n_takehata
1
150
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
170
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
32k
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
Featured
See All Featured
Making Projects Easy
brettharned
120
6.8k
The Invisible Side of Design
smashingmag
301
52k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Between Models and Reality
mayunak
4
500
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
330
Site-Speed That Sticks
csswizardry
13
1.6k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
820
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
940
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
560
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
Designing Powerful Visuals for Engaging Learning
tmiket
1
610
Transcript
Layout without Auto Layout for SwiftѪձ vol19
override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"
job = "iOS Developer" twitter = "@lovee" qiita = "@lovee" github = "el-hoshino" additionalInfo = "HITMAN ಈը໘ന͗ͯ͢ਐḿμϝͰ͢ʘ(^o^)ʗ" class Speaker: Developer { } }
গͰ ࣗ༝ʹൃදͰ͖Δ Swift Ѫձʹ ѹతײँ !
ࠓ·Ͱͷ Auto Layout dis
ࠓ·Ͱͷ Auto Layout dis
▸ Θ͔ΓͮΒ͍ ▸ σόοάͮ͠Β͍ ▸ ώΤϥϧΩʔ͕ͳ͍←͜Ε "VUP-BZPVUƎƇŧƉčἠದƑۋམƊżĎ Why Not Auto
Layout?
▸ Θ͔ΓͮΒ͍ ▸ σόοάͮ͠Β͍ ▸ ώΤϥϧΩʔ͕ͳ͍←͜Ε ▸ SnapKit ͱ͔Λ͏ख͋Δ͚ͲͬͺԶ AutoLayout
͕ ݏ͍ͩʂ Why Not Auto Layout?
▸ UIView ͷ layoutSubviews ΛΦʔόʔϥΠυ ▸ subviews ͷ frame Λઃఆ
▸ transform Λઃఆͨ͠߹ bounds.size / center ͷํΛઃఆ ▸ ࣗͷ frame ࣗͰઃఆ͠ͳ͍ʂ ▸ ඞཁʹԠͯ͡ setNeedsLayout() ͔ layoutIfNeeded() ΛݺͿ ▸ layoutSubviews() Λݺͳ͍ʂ ίʔυͰϨΠΞτͷεεϝ
▸ ඳըαΠΫϧͰ UIView ͷ layout ϑϥάΛࢹ ▸ layout ϑϥάΛཱͨͤΔ ▸
࣍ͷඳըαΠΫϧͰϑϥάཱ͕͍ͬͯΔ͜ͱ͕֬ೝ͞ΕɺlayoutSubviews() ͕ݺΕΔ ▸ layout ϑϥάΛཱͨͤΔखஈɿ ▸ ࣗͷ initʢͨΓલʣ ▸ ࣗͷ addSubviewʢͨΓલʣ ▸ subview ͷ frame ͷ setʢʮsubviewʯ ʹҙʂʣ ▸ setNeedsLayout() ΛݺͿ ▸ ͦͷଞ ▸ layoutIfNeeded() ΛݺͿͱ layout ϑϥάཱ͕͍ͬͯΕ͙͢ϨΠΞτ͢Δ ▸ ͭ·Γཱͬͯͳ͚ΕϨΠΞτ͠ͳ͍ʂ ▸ Ξχϝʔγϣϯͷ߹ setNeedsLayout() ݺΜͩޙΞχϝʔγϣϯઃఆʹ layoutIfNeeded() Λݺ͍͍ ※ࢀߟɿhttp://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/ ϨΠΞτͷྲྀΕʢུ֓ʣ
▸ ඳըαΠΫϧͰ UIView ͷ layout ϑϥάΛࢹ ▸ layout ϑϥάΛཱͨͤΔ ▸
࣍ͷඳըαΠΫϧͰϑϥάཱ͕͍ͬͯΔ͜ͱ͕֬ೝ͞ΕɺlayoutSubviews() ͕ݺΕΔ ▸ layout ϑϥάΛཱͨͤΔखஈɿ ▸ ࣗͷ initʢͨΓલʣ ▸ ࣗͷ addSubviewʢͨΓલʣ ▸ subview ͷ frame ͷ setʢʮsubviewʯ ʹҙʂʣ ▸ setNeedsLayout() ΛݺͿ ▸ ͦͷଞ ▸ layoutIfNeeded() ΛݺͿͱ layout ϑϥάཱ͕͍ͬͯΕ͙͢ϨΠΞτ͢Δ ▸ ͭ·Γཱͬͯͳ͚ΕϨΠΞτ͠ͳ͍ʂ ▸ Ξχϝʔγϣϯͷ߹ setNeedsLayout() ݺΜͩޙΞχϝʔγϣϯઃఆʹ layoutIfNeeded() Λݺ͍͍ ※ࢀߟɿhttp://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/ ϨΠΞτͷྲྀΕʢུ֓ʣ
ࣗͷ frame ࣗͰઃఆ͠ͳ͍ʂ ▸ UIKit ͷΈ্ɺࣗͷ frame ͷมߋϏϡʔͷ layout ϑϥάΛཱͨͤΔ͜ͱʹͳΓɺϏϡʔͷ
layout ઃఆͰ· ͨࣗͷ frame มߋ͕ߦΘΕΔͷͰɺඇޮ͚ͩͰͳ͘ɺ ߹ʹΑͬͯແݶϧʔϓʹͳΔڪΕ͋Δɻ ▸ ઃܭ্ɺࣗͷ frame ͋͘·Ͱ͔Βݟͨ߹ͷॴͱ େ͖͞ͳͷͰɺมߋͷݖݶΛ͖࣋ͭࣗͰͳ͘ɻ ϨΠΞτͷྲྀΕʢུ֓ʣ
layoutSubviews ͷΦʔόʔϥΠυ͕ਏ͍ ▸ UIView ΛαϒΫϥε͠ͳ͍ͱ͍͚ͳ͍ ▸ UIView ΛαϒΫϥεͯ͠ subview ͱ
layout ใͷؔ࿈͚͕؆୯ʹͰ ͖ΔͱݶΒͳ͍ ▸ subview Λ UIView ͷϓϩύςΟʔͱͯ͠ઃఆ͠ͳ͚ΕͳΒͳ͍ ▸ ͳͷʹଟ͘ͷ߹ subview Λ࡞ΔͷࣗͰͳ͍ʢओʹ UIViewControllerʣ ▸ layoutSubviews() ϝιου͕ΒΜͰಡΈͮΒ͍"
NotAutoLayout ※Download from here: https://github.com/el-hoshino/NotAutoLayout
None
None
None
NotAutoLayout ͷεεϝ 1.NotAutoLayout ରԠͷϏϡʔΛ࡞Δ 2.Ϗϡʔ͕ Layout Λ࡞Δ 3.ࢠϏϡʔΛՃͯ͠ઌ΄Ͳ࡞ͬͨ Layout ʹؔ࿈͚ͤ͞Δ
NotAutoLayout ରԠͷϏϡʔΛ࡞Δ let baseView = LayoutView(frame: CGRect(x: 0, y: 0,
width: 320, height: 568)) class BaseView: LayoutView { // ... } let baseView = BaseView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) public class BaseView: UIScrollView, LayoutControllable { public var layoutInfo: [LayoutControllable.Hash: [LayoutMethod]] = [:] public var orderInfo: [LayoutControllable.Hash : Int] = [:] public var zIndexInfo: [LayoutControllable.Hash: Int] = [:] public var layoutOptimization: LayoutOptimization = .sequence override public func layoutSubviews() { super.layoutSubviews() self.layoutControl() } // ... } let baseView = BaseView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
Ϗϡʔ͕ Layout Λ࡞Δ let layout = Layout.makeRelative(frame: CGRect(x: 0.2, y:
0.2, width: 0.6, height: 0.6)) let layout = Layout.makeInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)) let layout = Layout.makeCustom { (boundSize) -> Frame in return Frame(x: 0, from: .center, y: boundSize.height * 0.2, from: .top, width: 300, height: boundSize.height * 0.8) } ͳͲͳͲ…
ࢠϏϡʔΛՃͯ͠ઌ΄Ͳ࡞ͬͨ Layout ʹؔ࿈͚ͤ͞Δ baseView.addSubview(subview, constantLayout: layout) baseView.addSubview(subview, constantLayout: layout, order:
0, zIndex: -1)
DEMO
Frame ܕ ▸ CGRect ܕͱࣅͨΑ͏ͳͷ ▸ CGRect ʹʮج४ʢReferencePointʣʯͱ͍͏֓೦ΛՃ ▸ ج४ʹʮ.leftTopʯʮ.middleCenterʯͳͲͷ
9 ύλʔϯ Frame(x: 0, from: .center, y: 20, from: .top, width: 320, height: 480) ※NotAutoLayout Ͱʮج४ʯϏϡʔͱࢠϏϡʔ྆ํʹద༻͢Δɻ
Frame ܕ Frame(x: 0, from: .center, y: 20, from: .top,
width: 320, height: 480) 0 20 Frame(x: -20, from: .right, y: 0, from: .bottom, width: 320, height: 480) 0 -20 Frame(x: 0, from: .left, y: 0, from: .top, width: 320, height: 480) 0 0
ଞʹͨ͘͞Μ͋ΔͷͰ ͥͻݟͯΈ͍ͯͩ͘͞