Share
Embed
SwiftUIでUIViewを使うときのレイアウト処理 / Layout process when using UIKit view in SwiftUI
Mike Apurin
December 17, 2021
Transcript
21 slides
-
ࣗݾհ Apurin Mikhail ΞϓϦϯɾϛϋΠϧ auramagi apurin.me - ΏΊΈͷiOSΤϯδχΞ -
ࠓݱࡏϩγΞ͔ΒϦϞʔτϫʔΫ - ژ → ϞεΫϫͷఱؾΪϟοϓ͙͍͑ (+13° → -17°) - ିग़ͷM1 Max MacBook Proಧ͘લʹߦͬͨͷ͕੯͍͠ -
ࠓͷৼΓฦΓ ͳΜͱͳ͘SwiftUIΛ৮Δػձ͕ଟ͔ͬͨҰͩͬͨ - UIKitͷҊ݅ͰϓϨϏϡʔΛಋೖ - iOS 13.0·ͰҾ্͖͛Δ४උ - σϞΞϓϦͷ࡞ -
ΏΊΈࣾͷSwiftUIษڧձ։࠵ - ݸਓ։ൃ 🥰 -
SwiftUIͰUIKitͷϏϡʔ͕͍͍ͨ - εΫϥονͷҊ݅Ͱͳ͍ͳΒɺྑ͘ڞଘ͢Δඞཁ͕͋Δ - Apple͕४උ͞Ε͍ͯΔͷΛ͑Ͱ͖Δ - UIView → UIViewRepresentable -
UIViewController → UIViewControllerRepresentable -
struct LabelRepresentable: UIViewRepresentable { let text: String func makeUIView(context: Context)
-> UILabel { .init() } func updateUIView(_ uiView: UILabel, context: Context) { uiView.numberOfLines = 0 uiView.text = text } } UIViewRepresentableΛॻ͍ͯΈΔ ؆୯ʹॻ͚·͢ʂ ඞਢϝιου: - Ϗϡʔੜ makeUIView - Ϗϡʔߋ৽ updateUIView -
struct LabelRepresentable_Previews: PreviewProvider { static var previews: some View {
LabelRepresentable( text: "Journey before destination" ) } } ͬͨʂ ɻɻɻ Μʁ தԝἧ͑ʹͳ͍ͬͯͳ͍ʂʂ PreviewͰͬͯΈΔ -
struct LabelRepresentable_Previews: PreviewProvider { static var previews: some View {
LabelRepresentable( text: "Journey before destination" ) .border(.red) .padding() } } ը໘શମͷαΠζΛऔ͍ͬͯΔ SwiftUI ͷ Text ͷΑ͏ʹɺͽͬͨΓͷαΠζΛͱͬͯ தԝἧ͑ʹͳΔΠϝʔδ͕ͩͬͨɺ·͍͍͔͋ʁ PreviewͰͬͯΈΔ -
struct LabelRepresentable_Previews: PreviewProvider { static var previews: some View {
LabelRepresentable( text: "To love the journey is to accept no such end. I have found, through painful experience, that the most important step a person can take is always the next one." ) .border(.red) .padding() } } PreviewͰͬͯΈΔ Ξ ͩΊͩʔ orz -
SwiftUIͷϨΠΞτॲཧ WWDC 2019 — Building Custom Views with SwiftUI ͦͦSwiftUIͰͲ͏ͬͯҐஔͱαΠζ͕
ܾΊΒΕΔͷ͔ʁ ϨΠΞτॲཧͷৄࡉඇެ։ͳͷͰ υΩϡϝϯτ͕΄΅ͳ͍ 😔 جຊͷॲཧҎԼͷεςοϓ͔ΒͳΔ 1. View͕ࢠViewʹαΠζΛఏҊ͢Δ 2. ࢠView͕ࣗͷαΠζΛܾΊΔ 3. View͕ࣗͷ࠲ඪۭؒͰࢠViewΛஔ͢Δ -
SwiftUIͷϨΠΞτॲཧ ViewͷαΠζ࣍ͷܗʹͳ͍ͬͯΔͱਪଌͰ͖Δ struct Dimension { let min: CGFloat let ideal:
CGFloat let max: CGFloat } - min: ࠷ - ideal: ཧαΠζ - max: ࠷ߴ - ؔΛݫक: min <= ideal <= max -
SwiftUIͷϨΠΞτॲཧ ఏҊαΠζ࣍ͷܗͰ͞ΕΔͱਪଌͰ͖Δ struct ProposedSize { let width: CGFloat? let height:
CGFloat? } - ͕ࢦఆ͞Ε͍ͯΔ߹ɺͦΕΛϕʔεʹαΠζΛܭࢉ - nilͷ߹ɺViewࣗͷidealαΠζΛͱΔ - . fi xedSizeͰnilʹมߋ͞ΕΔ -
SwiftUIͷϨΠΞτॲཧ ௨ৗͷ߹ɺҎԼͷܭࢉ͕ࣜదԠ͞ΕΔ if let proposedWidth = proposedSize.width { // dimensionsͰ্ݶɾԼݶΛ͑Δ
width = max( dimensions.min, min(dimensions.max, proposedWidth) ) } else { width = dimensions.ideal } // heightಉ༷ -
Color.mint .frame( minWidth: 100, maxWidth: 200, minHeight: 100, maxHeight: 200
) .frame(width: 300, height: 300) .border(.mint) SwiftUIͷϨΠΞτॲཧ 📱 W: 390 H: 763 .frame(minWidth: 200, …) .frame(width: 300, height: 300) Color ఏҊαΠζ ࣮αΠζ W: 300 H: 300 W: 200 H: 200 W: 300 H: 300 W: 200 H: 200 W: 200 H: 200 -
Text("Hello YUMEMI.swift") .fixedSize() .border(.mint) .frame(width: 300, height: 300) .border(.mint) SwiftUIͷϨΠΞτॲཧ
📱 W: 390 H: 763 .fixedSize() .frame(width: 300, height: 300) Text("Hello YUMEMI.swift") ఏҊαΠζ ࣮αΠζ W: 300 H: 300 W: nil H: nil W: 300 H: 300 W: 149.7 H: 20.3 W: 149.7 H: 20.3 -
UIViewRepresentable AutoLayoutͷintrinsicαΠζͱpriorityΛݩʹͯ͠αΠζΛܭࢉ Intrinsic size Compression Resistance Hugging ݁Ռ (min …
ideal … max) (-1) - - 0 … 0 … ∞ x < 750 < 750 0 … x … ∞ x < 750 >= 750 0 … x … x x < 750 < 750 x … x … ∞ x >= 750 >= 750 x … x … x -1 = UIView.noIntrinsicMetric 750 = UILayoutPriority.defaultHigh -
UIViewRepresentable 1 ͱ 2 ͷڍಈௐͰ͖Δඇެ։API͕͋Δ ⚠ ඇެ։APIͳͷͰϦϦʔεϏϧυʹೖΕͪΌμϝ ⚠ public protocol
UIViewRepresentable { // 1 (AutoLayoutϓϩύςΟ → ͳΕΔαΠζ) func _overrideLayoutTraits( _ layoutTraits: inout SwiftUI._LayoutTraits, for uiView: Self.UIViewType ) // 2 (ఏҊαΠζ → ࣮αΠζ) func _overrideSizeThatFits( _ size: inout CoreGraphics.CGSize, in proposedSize: SwiftUI._ProposedSize, uiView: Self.UIViewType ) } -
import RepresentableKit struct UILabel_Previews: PreviewProvider { static var previews: some
View { UIViewAdaptor { let view = UILabel() view.numberOfLines = 0 view.text = "To love the journey is to accept no such end. I have found, through painful experience, that the most important step a person can take is always the next one." return view } .padding() } } RepresentableKitͷհ UIViewRepresentableͷαΠζܭࢉΛநԽͯ͠ ΑΓ؆୯ʹ͑ΔΑ͏ʹϔϧύʔϥΠϒϥϦΛ ࡞Γ·ͨ͠ɻ https://github.com/yumemi-inc/RepresentableKit