SwiftUI + UIKit

SwiftUI 2023. 5. 10. 11:45
반응형
  • 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)
}

 

 

 

 

반응형
Posted by 까칠코더
,