我试图将SubView SwiftUI 视图添加到UIView。 self.view.addSubview(contentView)
错误:无法将“ContentView”类型的值转换为预期的参数类型“UIView”
请帮我实现这个 UI。
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = UIColor.lightGray
let contentView = ContentView()
view.addSubview(contentView) // Error: Cannot convert value of type 'ContentView' to expected argument type 'UIView'
}
}
struct ContentView: View {
var body: some View {
Text("Hello world")
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试UITableView在 SwiftUI 应用程序中使用
struct UIList: UIViewRepresentable {
var rows: [String]
func makeUIView(context: Context) -> UITableView {
let collectionView = UITableView(frame: .zero, style: .plain)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.dataSource = context.coordinator
collectionView.delegate = context.coordinator
collectionView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
return collectionView
}
func updateUIView(_ uiView: UITableView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(rows: rows)
}
class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
var rows: [String]
init(rows: [String]) {
self.rows = rows
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何删除当我UIHostingConfiguration在UICollectionView. 例如使用以下代码:
https://github.com/mvemjsun/CollectionView
如果我添加边框CellView:
/// Create a cell registration of type UICollectionViewCell with `ToDoListItem` that will be used to provide the collection view cells.
/// This will
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewCell, ToDoListItem> { cell, indexPath, item in
cell.contentConfiguration = UIHostingConfiguration {
CellView(toDoListItem: item)
.border(.red)// <-- I want this border to match the blue border...
}
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.blue.cgColor //<-- this shows the actual cell size I want my swiftui view to …Run Code Online (Sandbox Code Playgroud)