Skip to content

Instantly share code, notes, and snippets.

@koher
Created January 26, 2021 17:08
Show Gist options
  • Save koher/13506ea9c45ebfd8b04249a07b23828f to your computer and use it in GitHub Desktop.
Save koher/13506ea9c45ebfd8b04249a07b23828f to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView {
MyView()
Text("Hello")
}
}
}
import SwiftUI
struct MyView: View {
@State private var size: CGSize = .zero
var body: some View {
_MyView(size: $size)
.frame(width: size.width, height: size.height)
}
}
private struct _MyView: UIViewControllerRepresentable {
@Binding var size: CGSize
func makeUIViewController(context: Context) -> MyViewController {
let viewController = MyViewController()
viewController.size = $size
return viewController
}
func updateUIViewController(_ uiViewController: MyViewController, context: Context) {
}
}
private final class MyViewController: UIViewController {
var size: Binding<CGSize>!
private let subview: UIView = .init()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
subview.translatesAutoresizingMaskIntoConstraints = false
subview.backgroundColor = .blue
view.addSubview(subview)
NSLayoutConstraint.activate([
subview.widthAnchor.constraint(equalToConstant: 200),
subview.heightAnchor.constraint(equalToConstant: 200),
subview.leadingAnchor.constraint(equalTo: view.leadingAnchor),
subview.topAnchor.constraint(equalTo: view.topAnchor),
])
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
size.wrappedValue = subview.bounds.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment