Last active
April 6, 2020 13:08
-
-
Save Jerrot/fbb89c51e049625892f6ebc82718d843 to your computer and use it in GitHub Desktop.
Use Xcode 11 UI Preview feature for UIKit Views
This file contains hidden or 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 SwiftUI | |
final class SomeView: UIView { | |
init() { | |
super.init(frame: CGRect.zero) | |
// TODO: create your view content | |
} | |
@available(*, unavailable) | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
final class SomeViewController: UIViewController { | |
let someView = SomeView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setup() | |
} | |
func setup() { | |
someView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(someView) | |
// TODO: adapt to your needs | |
someView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
someView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true | |
someView.widthAnchor.constraint(equalToConstant: 400.0).isActive = true | |
someView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true | |
} | |
} | |
extension SomeViewController: UIViewControllerRepresentable { | |
typealias UIViewControllerType = SomeViewController | |
@available(iOS 13.0, *) | |
func makeUIViewController(context: UIViewControllerRepresentableContext<UIViewControllerType>) -> UIViewControllerType { | |
return SomeViewController() | |
} | |
@available(iOS 13.0, *) | |
func updateUIViewController(_ uiViewController: UIViewControllerType, context: UIViewControllerRepresentableContext<UIViewControllerType>) {} | |
} | |
@available(iOS 13, *) | |
struct SomeViewController_Previews: PreviewProvider { | |
static var previews: some View { | |
SomeViewController() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment