|
| 1 | +# 240827 UIViewRepresentable, UIViewControllerRepresentable |
| 2 | + |
| 3 | +SwiftUI ํ๊ฒฝ์์ UIKit ์์๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ. |
| 4 | + |
| 5 | + |
| 6 | +8์ 27์ผ (ํ) |
| 7 | + |
| 8 | + |
| 9 | +# ํ์ต๋ด์ฉ |
| 10 | + |
| 11 | +ํ๋ก์ ํธ ๋ด์์ ์ผ๋ถ ํ๋ฉด์ SwiftUI๋ฅผ ๋ฒ ์ด์ค๋ก ๊ตฌํํ ์ ์์ ์ง ๊ธฐ์ ๊ฒํ ๋ฅผ ํ๋ ์์ค์... ์ต์๋ฒ์ `iOS 15.0`์์๋ ๊ธฐ์กด `UIScrollView`์ ์ค์ ๊ธฐ๋ฅ์ด SwiftUI์ `ScrollView`์๋ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์์ UIScrollView๋ฅผ ๊ฐ์ ธ๋ค๊ฐ ์ฌ์ฉํค์ผํ๋ ์ํฉ์ ๋ง๋ฌ๋ค. |
| 12 | + |
| 13 | +์ฐ๋ฆฌ ํ๋ก์ ํธ ๊ตฌ์กฐ์์๋ ์ต์๋ฒ์ `iOS 18.0`์ ๋์ผ ์คํฌ๋กค๋ทฐ๊ฐ ๊ทธ๋๋ง ์ธ๋งํ๋ค๋ ํ๋จ์ด ๋ค์๊ณ , ๊ทธ์ ๊น์ง๋ UIScrollView๋ฅผ SwiftUI๋ก ๋ํํ์ฌ ์ง์ ํ์ํ ๊ธฐ๋ฅ์ ๊ตฌํํ ํ ์ฌ์ฉํด์ผํ๋ค๋ ์ฌ์ค์ ์๊ฒ๋์๋ค. |
| 14 | + |
| 15 | +๊ทธ๋์ UIKit์ ์์๋ฅผ SwiftUI๋ก ๊ฐ์ ธ์ ์ฌ์ฉํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ๋์ง ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด๊ฒ ๋์๋ค. |
| 16 | + |
| 17 | +## UIViewRepresentable |
| 18 | + |
| 19 | +* UIKit์ `UIView`๋ฅผ SwiftUI์์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ํ๋กํ ์ฝ |
| 20 | + |
| 21 | +### ํ์ ๊ตฌํ ๋ฉ์๋ |
| 22 | + |
| 23 | +* `makeUIView(context:)` |
| 24 | + * SwiftUI์์ ์ฌ์ฉํ UIView ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. |
| 25 | + * UILabel, UIButton ๊ฐ์ UIKit์ ๋ทฐ๋ฅผ ์ด ๋ฉ์๋์์ ์์ฑํด์ฃผ๋ฉด ๋๋ค. |
| 26 | +* `updateUIView(_:context:)` |
| 27 | + * SwiftUI์ ์ํ์ ๋ฐ๋ผ UIView๋ฅผ ์
๋ฐ์ดํธ ํ๋ค. |
| 28 | + * ์์ฑ ๋ํผ ๊ฐ์ด ์
๋ฐ์ดํธ ๋๋ฉด, ํด๋น ๋ฉ์๋๊ฐ ๋ถ๋ ค์ง๋ค. |
| 29 | + |
| 30 | +```swift |
| 31 | +struct MyCustomLabel: UIViewRepresentable { |
| 32 | + @Binding var text: String // ์ธ๋ถ์์ ์ด ๊ฐ์ด ๋ฐ๋๋ฉด.. |
| 33 | + |
| 34 | + func makeUIView(context: Context) -> UILabel { |
| 35 | + let label = UILabel() |
| 36 | + label.text = text |
| 37 | + return label |
| 38 | + } |
| 39 | + |
| 40 | + // ์ด ๋ฉ์๋๊ฐ ๋ถ๋ ค์ง๋ค. |
| 41 | + func updateUIView(_ uiView: UILabel, context: Context) { |
| 42 | + uiView.text = text |
| 43 | + // ์
๋ฐ์ดํธ๋ฅผ ์ค์ ๋ก ํ ์ง ๋ง์ง๋ ๊ตฌํ์์ ๋ชซ์ด๋ค. |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## UIViewControllerRepresentable |
| 49 | + |
| 50 | +* UIKit์ UIViewController๋ฅผ SwiftUI์์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ํ๋กํ ์ฝ |
| 51 | + |
| 52 | +### ํ์ ๊ตฌํ ๋ฉ์๋ |
| 53 | + |
| 54 | +* `makeUIViewController(context:)` |
| 55 | + * UIViewController ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. |
| 56 | + * UIImagePickerController ๊ฐ์ ์ปค์คํ
๋ทฐ ์ปจํธ๋กค๋ฌ๋ฅผ ์ด ๋ฉ์๋์์ ์์ฑํด์ฃผ๋ฉด ๋๋ค. |
| 57 | +* `updateUIViewController(_:context:)` |
| 58 | + * SwiftUI์ ์ํ์ ๋ฐ๋ผ UIViewController๋ฅผ ์
๋ฐ์ดํธ ํ๋ค. |
| 59 | + |
| 60 | +```swift |
| 61 | +struct MyCustomViewController: UIViewControllerRepresentable { |
| 62 | + func makeUIViewController(context: Context) -> UIViewController { |
| 63 | + let viewController = MyViewController() |
| 64 | + return viewController |
| 65 | + } |
| 66 | + |
| 67 | + func updateUIViewController(_ uiViewController: UIViewController, context: Context) { |
| 68 | + // ํ์ํ ๊ฒฝ์ฐ UIViewController๋ฅผ ์
๋ฐ์ดํธ |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +## ๋๋ ์ |
| 75 | + |
| 76 | +* ์์ง SwiftUI์ ๊ธฐ๋ณธ ์ปดํฌ๋ํธ๋ง์ผ๋ก๋ ๋ณต์กํ ๊ตฌ์กฐ์ ์ฑ์ ๋ค๋ฃจ๊ธฐ๋ ํ๊ณ๊ฐ ์๋ค. (iOS 15, 16 ๊ธฐ์ค...) |
| 77 | + * ๋ญ๋์ ์คํฌ๋กค๋ทฐ๊ฐ ํ์ด์ง ๊ธฐ๋ฅ๋ ์๊ณ ... offset ์ถ์ ํ๋ ๊ธฐ๋ฅ๋ ์๋.... `TabView` ์ฐ๊ฑฐ๋, `offset` ์ถ์ ๊ธฐ๋ฅ์ ์ง์ ๋ง๋ค์ด์ผํจ. |
| 78 | +* ๊ทธ๋์ ๊ฒฐ๊ตญ์ UIKit ์ปดํฌ๋ํธ๋ฅผ ์์ ๊ฐ์ ํ๋กํ ์ฝ์ ํตํด ๊ตฌํํ ํ ์ฌ์ฉ์ด ํ์ํ ๊ฒฝ์ฐ๊ฐ ์ข
์ข
์์ ๊ฒ ๊ฐ๋ค. |
| 79 | +* ์์ง์... ์ฐ๋ฆฌ ํ๋ก์ ํธ์์๋ ๊ฐ๋จํ ํ๋ฉด์ด๋ ๋ทฐ ์ปดํฌ๋ํธ๋ SwiftUI๋ก ๊ตฌํํด๋ณผ๋ง ํ ๊ฒ ๊ฐ์ง๋ง, ์ปฌ๋ ์
๋ทฐ๋ ์คํฌ๋กค๋ทฐ๋ฅผ ํตํ ๋ณต์กํ ํ๋ฉด ๋ฒ ์ด์ค๋ UIKit์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฆฌ์์ค๊ฐ ๋ ๋ค ๊ฒ ๊ฐ๋ค. |
| 80 | + |
| 81 | +๋ค์์ SwiftUI์ ์์๋ฅผ UIKit์์ ์ฌ์ฉํ ๋ ์ด๋ค ์์ผ๋ก ์ฌ์ฉํด๋ณด๋ฉด ์ข์ ์ง ์์๋ด์ผ๊ฒ ๋ค. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | + |
| 86 | +# ์ฐธ๊ณ ๋งํฌ |
| 87 | + |
| 88 | +- [https://developer.apple.com/documentation/swiftui/uiviewrepresentable](https://developer.apple.com/documentation/swiftui/uiviewrepresentable) |
| 89 | +- [https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable](https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable) |
| 90 | +- [https://gist.github.com/aronbalog/2fade2ae3f9fa61dff0854aa661d20a6](https://gist.github.com/aronbalog/2fade2ae3f9fa61dff0854aa661d20a6) |
| 91 | +- [https://gist.github.com/mbernson/9090953d3f5ca4f129eb72ea58436fdd](https://gist.github.com/mbernson/9090953d3f5ca4f129eb72ea58436fdd) |
| 92 | +- [iOS 18 ์ฌ๋ฆฌ๋ฉด ๊ทธ๋ ๋ค์ ๋ณด์...](https://developer.apple.com/documentation/realitykit/model3d/onscrollvisibilitychange(threshold:_:)) |
0 commit comments