Created
January 26, 2021 17:08
-
-
Save koher/13506ea9c45ebfd8b04249a07b23828f to your computer and use it in GitHub Desktop.
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 SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
ScrollView { | |
MyView() | |
Text("Hello") | |
} | |
} | |
} |
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 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