Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created April 19, 2020 00:19
Show Gist options
  • Save azamsharp/1dcc2f1895849e47d74833d6983bd24a to your computer and use it in GitHub Desktop.
Save azamsharp/1dcc2f1895849e47d74833d6983bd24a to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// DemoHostingC
//
// Created by Mohammad Azam on 4/18/20.
// Copyright © 2020 Mohammad Azam. All rights reserved.
//
import SwiftUI
struct ContentView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
.background(Color.green)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// ViewController.swift
// DemoHostingC
//
// Created by Mohammad Azam on 4/18/20.
// Copyright © 2020 Mohammad Azam. All rights reserved.
//
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let controller = UIHostingController(rootView: ContentView())
controller.view.translatesAutoresizingMaskIntoConstraints = false
self.addChild(controller)
self.view.addSubview(controller.view)
controller.didMove(toParent: self)
NSLayoutConstraint.activate([
controller.view.widthAnchor.constraint(equalToConstant: 200),
controller.view.heightAnchor.constraint(equalToConstant: 44),
controller.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
controller.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment