반응형
- SwiftUI에서 UIKit UIScrollView 사용하기
import SwiftUI
struct CustomScrollView: UIViewRepresentable {
var text: String
func makeUIView(context: UIViewRepresentableContext<CustomScrollView>) -> UlScrollView {
let scrollView = UIScrollView()
scrollView.delegate = context.coordinator
scrollView.refreshControl = UIRefreshControl()
scrollView.refreshControl?.addTarget(context.coordinator, action: #selector(Coordinator.handleRefresh), for: .valueChanged)
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
titleLabel.text = text
titleLabel.textAlignment = .center
scrollView.addSubview(titleLabel)
return scrollView
}
func makeCoordinator() ~> Coordinator {
Coordinator(self)
}
func updateUIView(_ uiView: UlScrollView, context: UIViewRepresentableContext<CustomScrollView>) {
}
class Coordinator: NSObject, UlScrollViewDelegate {
var control: CustomScrollView
init(_ control: CustomScrollView) {
self.control = control
}
func scrollViewDidScroll(_ scrollView: UlScrollView) {
}
@objc func handleRefresh(sender: UIRefreshControl) {
sender.endRefreshing()
}
}
}
// How to use
struct ContentView: View {
var body: some View {
CustomScrollView(text: "UlScrollView in SwiftUI")
}
}
- SwiftUI에서 UIKit의 UIImagePickerController 사용하기
import SwiftUI
typealias PickerActionHandler = ((Image) -> Void)
struct ImagePicker: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController
@Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
let sourceType: UIImagePickerController.SourceType
let action: PickerActionHandler
func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let control: ImagePicker
init(_ control: ImagePicker) {
self.control = control
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
control.action(Image(uiImage: image))
control.presentationMode.wrappedValue.dismiss()
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
control.presentationMode.wrappedValue.dismiss()
}
}
}
// How to use
struct ProfileView: View {
@State private var showingImagePicker = false
@State private var pickedImage: Image?
var body: some View {
Button(action: {
self.showingImagePicker.toggle()
}, label: {
Text("update profile photo")
})
.sheet(isPresented: $showingImagePicker) {
ImagePicker(sourceType: .photoLibrary) { (image) in
pickedImage = image
}
}
}
}
- UIKit에서 SwiftUI의 View 사용하기
override func viewDidLoad() {
super.viewDidLoad()
let swiftUIController = UIHostingController(rootView: SwiftUITestView() )
addChild(swiftUIController)
view.addSubview(swiftUIController.view)
swiftUIController.didMove(toParent: self)
}
반응형
'SwiftUI' 카테고리의 다른 글
Using TimelineView and Canvas in SwiftUI (0) | 2022.01.27 |
---|---|
Understanding Data Flow in SwiftUI (0) | 2021.01.05 |
How to Create a Neumorphic Design With SwiftUI (1) | 2020.06.01 |
SwiftUI를 사용해서 Apple 로그인하기(Sign in with Apple using SwiftUI) (0) | 2019.10.08 |
How to Create a Splash Screen With SwiftUI (0) | 2019.09.06 |
SwiftUI (0) | 2019.07.23 |
SwiftUI: Getting Started (0) | 2019.06.27 |