-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 240827_UIViewRepresentable,_UIViewControllerRepresentable.md
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...e/TIL.docc/SwiftUI/240827_UIViewRepresentable,_UIViewControllerRepresentable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# 240827 UIViewRepresentable, UIViewControllerRepresentable | ||
|
||
SwiftUI ํ๊ฒฝ์์ UIKit ์์๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ. | ||
|
||
|
||
8์ 27์ผ (ํ) | ||
|
||
|
||
# ํ์ต๋ด์ฉ | ||
|
||
ํ๋ก์ ํธ ๋ด์์ ์ผ๋ถ ํ๋ฉด์ SwiftUI๋ฅผ ๋ฒ ์ด์ค๋ก ๊ตฌํํ ์ ์์ ์ง ๊ธฐ์ ๊ฒํ ๋ฅผ ํ๋ ์์ค์... ์ต์๋ฒ์ `iOS 15.0`์์๋ ๊ธฐ์กด `UIScrollView`์ ์ค์ ๊ธฐ๋ฅ์ด SwiftUI์ `ScrollView`์๋ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์์ UIScrollView๋ฅผ ๊ฐ์ ธ๋ค๊ฐ ์ฌ์ฉํค์ผํ๋ ์ํฉ์ ๋ง๋ฌ๋ค. | ||
|
||
์ฐ๋ฆฌ ํ๋ก์ ํธ ๊ตฌ์กฐ์์๋ ์ต์๋ฒ์ `iOS 18.0`์ ๋์ผ ์คํฌ๋กค๋ทฐ๊ฐ ๊ทธ๋๋ง ์ธ๋งํ๋ค๋ ํ๋จ์ด ๋ค์๊ณ , ๊ทธ์ ๊น์ง๋ UIScrollView๋ฅผ SwiftUI๋ก ๋ํํ์ฌ ์ง์ ํ์ํ ๊ธฐ๋ฅ์ ๊ตฌํํ ํ ์ฌ์ฉํด์ผํ๋ค๋ ์ฌ์ค์ ์๊ฒ๋์๋ค. | ||
|
||
๊ทธ๋์ UIKit์ ์์๋ฅผ SwiftUI๋ก ๊ฐ์ ธ์ ์ฌ์ฉํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ๋์ง ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด๊ฒ ๋์๋ค. | ||
|
||
## UIViewRepresentable | ||
|
||
* UIKit์ `UIView`๋ฅผ SwiftUI์์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ํ๋กํ ์ฝ | ||
|
||
### ํ์ ๊ตฌํ ๋ฉ์๋ | ||
|
||
* `makeUIView(context:)` | ||
* SwiftUI์์ ์ฌ์ฉํ UIView ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. | ||
* UILabel, UIButton ๊ฐ์ UIKit์ ๋ทฐ๋ฅผ ์ด ๋ฉ์๋์์ ์์ฑํด์ฃผ๋ฉด ๋๋ค. | ||
* `updateUIView(_:context:)` | ||
* SwiftUI์ ์ํ์ ๋ฐ๋ผ UIView๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค. | ||
* ์์ฑ ๋ํผ ๊ฐ์ด ์ ๋ฐ์ดํธ ๋๋ฉด, ํด๋น ๋ฉ์๋๊ฐ ๋ถ๋ ค์ง๋ค. | ||
|
||
```swift | ||
struct MyCustomLabel: UIViewRepresentable { | ||
@Binding var text: String // ์ธ๋ถ์์ ์ด ๊ฐ์ด ๋ฐ๋๋ฉด.. | ||
|
||
func makeUIView(context: Context) -> UILabel { | ||
let label = UILabel() | ||
label.text = text | ||
return label | ||
} | ||
|
||
// ์ด ๋ฉ์๋๊ฐ ๋ถ๋ ค์ง๋ค. | ||
func updateUIView(_ uiView: UILabel, context: Context) { | ||
uiView.text = text | ||
// ์ ๋ฐ์ดํธ๋ฅผ ์ค์ ๋ก ํ ์ง ๋ง์ง๋ ๊ตฌํ์์ ๋ชซ์ด๋ค. | ||
} | ||
} | ||
``` | ||
|
||
## UIViewControllerRepresentable | ||
|
||
* UIKit์ UIViewController๋ฅผ SwiftUI์์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ํ๋กํ ์ฝ | ||
|
||
### ํ์ ๊ตฌํ ๋ฉ์๋ | ||
|
||
* `makeUIViewController(context:)` | ||
* UIViewController ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. | ||
* UIImagePickerController ๊ฐ์ ์ปค์คํ ๋ทฐ ์ปจํธ๋กค๋ฌ๋ฅผ ์ด ๋ฉ์๋์์ ์์ฑํด์ฃผ๋ฉด ๋๋ค. | ||
* `updateUIViewController(_:context:)` | ||
* SwiftUI์ ์ํ์ ๋ฐ๋ผ UIViewController๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค. | ||
|
||
```swift | ||
struct MyCustomViewController: UIViewControllerRepresentable { | ||
func makeUIViewController(context: Context) -> UIViewController { | ||
let viewController = MyViewController() | ||
return viewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | ||
// ํ์ํ ๊ฒฝ์ฐ UIViewController๋ฅผ ์ ๋ฐ์ดํธ | ||
} | ||
} | ||
``` | ||
|
||
|
||
## ๋๋ ์ | ||
|
||
* ์์ง SwiftUI์ ๊ธฐ๋ณธ ์ปดํฌ๋ํธ๋ง์ผ๋ก๋ ๋ณต์กํ ๊ตฌ์กฐ์ ์ฑ์ ๋ค๋ฃจ๊ธฐ๋ ํ๊ณ๊ฐ ์๋ค. (iOS 15, 16 ๊ธฐ์ค...) | ||
* ๋ญ๋์ ์คํฌ๋กค๋ทฐ๊ฐ ํ์ด์ง ๊ธฐ๋ฅ๋ ์๊ณ ... offset ์ถ์ ํ๋ ๊ธฐ๋ฅ๋ ์๋.... `TabView` ์ฐ๊ฑฐ๋, `offset` ์ถ์ ๊ธฐ๋ฅ์ ์ง์ ๋ง๋ค์ด์ผํจ. | ||
* ๊ทธ๋์ ๊ฒฐ๊ตญ์ UIKit ์ปดํฌ๋ํธ๋ฅผ ์์ ๊ฐ์ ํ๋กํ ์ฝ์ ํตํด ๊ตฌํํ ํ ์ฌ์ฉ์ด ํ์ํ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์์ ๊ฒ ๊ฐ๋ค. | ||
* ์์ง์... ์ฐ๋ฆฌ ํ๋ก์ ํธ์์๋ ๊ฐ๋จํ ํ๋ฉด์ด๋ ๋ทฐ ์ปดํฌ๋ํธ๋ SwiftUI๋ก ๊ตฌํํด๋ณผ๋ง ํ ๊ฒ ๊ฐ์ง๋ง, ์ปฌ๋ ์ ๋ทฐ๋ ์คํฌ๋กค๋ทฐ๋ฅผ ํตํ ๋ณต์กํ ํ๋ฉด ๋ฒ ์ด์ค๋ UIKit์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฆฌ์์ค๊ฐ ๋ ๋ค ๊ฒ ๊ฐ๋ค. | ||
|
||
๋ค์์ SwiftUI์ ์์๋ฅผ UIKit์์ ์ฌ์ฉํ ๋ ์ด๋ค ์์ผ๋ก ์ฌ์ฉํด๋ณด๋ฉด ์ข์ ์ง ์์๋ด์ผ๊ฒ ๋ค. | ||
|
||
--- | ||
|
||
|
||
# ์ฐธ๊ณ ๋งํฌ | ||
|
||
- [https://developer.apple.com/documentation/swiftui/uiviewrepresentable](https://developer.apple.com/documentation/swiftui/uiviewrepresentable) | ||
- [https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable](https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable) | ||
- [https://gist.github.com/aronbalog/2fade2ae3f9fa61dff0854aa661d20a6](https://gist.github.com/aronbalog/2fade2ae3f9fa61dff0854aa661d20a6) | ||
- [https://gist.github.com/mbernson/9090953d3f5ca4f129eb72ea58436fdd](https://gist.github.com/mbernson/9090953d3f5ca4f129eb72ea58436fdd) | ||
- [iOS 18 ์ฌ๋ฆฌ๋ฉด ๊ทธ๋ ๋ค์ ๋ณด์...](https://developer.apple.com/documentation/realitykit/model3d/onscrollvisibilitychange(threshold:_:)) |