chaneeii / iOS-Study-Log

✨ iOS에 대해 공부한 것들을 기록합니다 ✨
18 stars 0 forks source link

SwiftUI 를 사용해서 UIKit 에서 Preview 보기 #10

Open chaneeii opened 2 years ago

chaneeii commented 2 years ago

Nib 파일을 Preview 한 파일

//#if DEBUG
import SwiftUI

struct AlarmRepresentable: UIViewRepresentable {
    typealias UIViewType = AlarmListCell

    func makeUIView(context: Context) -> AlarmListCell {
//        let xib = UINib(nibName: "AlramListCell", bundle: nil)
//        return AlarmListCell()
        return UINib(nibName: "AlramListCell", bundle: nil)
                                    .instantiate(withOwner: nil, options: nil).first as! AlarmListCell
    }

    func updateUIView(_ uiView: AlarmListCell, context: Context) {}
}

struct AlarmPreview: PreviewProvider {
    static var previews: some View {
        AlarmRepresentable()
            .frame(width: 340, height: 123)
            .previewLayout(.sizeThatFits)
    }
}
//#endif

SwiftUI Preview

import SwiftUI

struct HomeViewController_Previews: PreviewProvider {

    static var previews: some View {
        Container().edgesIgnoringSafeArea(.all)
    }

    struct Container: UIViewControllerRepresentable {

        func makeUIViewController(context: Context) -> UIViewController {
            let layout = UICollectionViewLayout()
            let homeViewController = HomeViewController(collectionViewLayout: layout)
            return UINavigationController(rootViewController: homeViewController)
        }

        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}

        typealias UIViewControllerType = UIViewController

    }

}
chaneeii commented 2 years ago

Preview 가 안뜨는경우

스크린샷 2022-08-17 오후 3 59 52