Skip to content

Commit

Permalink
Create 240827_UIViewRepresentable,_UIViewControllerRepresentable.md
Browse files Browse the repository at this point in the history
  • Loading branch information
leeari95 committed Aug 27, 2024
1 parent 0616c68 commit 41604ce
Showing 1 changed file with 92 additions and 0 deletions.
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:_:))

0 comments on commit 41604ce

Please sign in to comment.