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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
550
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
550
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
160
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
400
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
780
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への指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
240
Heart of Swift Concurrency
koher
0
1k
JRuby: Past, Present, and Future
headius
0
210
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
540
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
260
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
140
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
140
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
160
速習iPhone Duo対応
yuukiw00w
2
840
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
110
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
270
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
530
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Being A Developer After 40
akosma
91
590k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
580
Six Lessons from altMBA
skipperchong
29
4.5k
How to make the Groovebox
asonas
2
2.5k
First, design no harm
axbom
PRO
2
1.3k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
260
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Automating Front-end Workflow
addyosmani
1369
210k
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
ଞʹͨ͘͞Μ͋ΔͷͰ ͥͻݟͯΈ͍ͯͩ͘͞