//Xcode SwiftUI转UIKit界面后,界面大小异常,所有组件变得比之前大
转换代码:
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Hiding Nav Bar...
//self.navigationController?.navigationBar.isHidden = true
extractView()
}
//Extracting SwiftUI View And Setting to UIkit View...
func extractView(){
let hostView = UIHostingController(rootView: Home())
hostView.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(hostView.view)
// Constraints...
// Clipping SwiftUI View to UIkit View...
let constraints = [
hostView.view.topAnchor.constraint(equalTo: view.topAnchor),
hostView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
hostView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
// Height And Width...
hostView.view.widthAnchor.constraint(equalTo: view.widthAnchor),
hostView.view.heightAnchor.constraint(equalTo: view.heightAnchor),
]
self.view.addConstraints(constraints)
}
}