Forked from mattt/UIViewControllerPreview.swift
Last active
October 21, 2019 07:14
-
-
Save VadimYakovliev/ab025c4ff7945d07d71bd2b9e4f0e664 to your computer and use it in GitHub Desktop.
Generic structures to host previews of UIView and UIViewController subclasses.
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
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
let deviceNames: [String] = [ | |
"iPhone SE", | |
"iPad 11 Pro Max", | |
"iPad Pro (11-inch)" | |
] | |
@available(iOS 13.0, *) | |
struct ViewController_Preview: PreviewProvider { | |
static var previews: some View { | |
ForEach(deviceNames, id: \.self) { deviceName in | |
UIViewControllerPreview { | |
UIStoryboard(name: "Main", bundle: nil) | |
.instantiateInitialViewController { coder in | |
ViewController(coder: coder) | |
}! | |
}.previewDevice(PreviewDevice(rawValue: deviceName)) | |
.previewDisplayName(deviceName) | |
} | |
} | |
} | |
#endif |
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
Group { | |
UIViewPreview { | |
let button = FavoriteButton(frame: .zero) | |
return button | |
} | |
UIViewPreview { | |
let button = FavoriteButton(frame: .zero) | |
button.isFavorited = true | |
return button | |
} | |
}.previewLayout(.sizeThatFits) | |
.padding(10) |
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
import UIKit | |
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
@available(iOS 13.0, *) | |
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable { | |
let viewController: ViewController | |
init(_ builder: @escaping () -> ViewController) { | |
viewController = builder() | |
} | |
// MARK: - UIViewControllerRepresentable | |
func makeUIViewController(context: Context) -> ViewController { | |
viewController | |
} | |
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<UIViewControllerPreview<ViewController>>) { | |
return | |
} | |
} | |
#endif |
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
import UIKit | |
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
@available(iOS 13.0, *) | |
struct UIViewPreview<View: UIView>: UIViewRepresentable { | |
let view: View | |
init(_ builder: @escaping () -> View) { | |
view = builder() | |
} | |
// MARK: - UIViewRepresentable | |
func makeUIView(context: Context) -> UIView { | |
return view | |
} | |
func updateUIView(_ view: UIView, context: Context) { | |
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | |
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment