- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试在 UIViewControllerRepresentable 中实现 UITableViewController,其中单元格的内容又是 SwiftUI View 。我无法使用 SwiftUI 列表,因为我想稍后添加 UISearchController。
因为我希望能够将自定义 SwiftUI View 作为每个单元格的内容,所以如果单元格内没有 SwiftUI View ,我不可能做到这一点。
我当前的代码不起作用,如下所示:
class SearchableListCell: UITableViewCell {
let contentController: UIViewController
init(withContent content: UIViewController, reuseIdentifier: String) {
self.contentController = content
super.init(style: .default, reuseIdentifier: reuseIdentifier)
self.addSubview(self.contentController.view)
// Tried also
// self.contentView.addSubview(self.contentController.view)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct SearchableList: UIViewControllerRepresentable {
let data: [String]
var viewBuilder: (String) -> ContentView
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> UITableViewController {
return context.coordinator.tableViewController
}
func updateUIViewController(_ tableViewController: UITableViewController, context: Context) {
}
class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
var parent: SearchableList
let tableViewController = UITableViewController()
init(_ searchableList: SearchableList) {
self.parent = searchableList
super.init()
tableViewController.tableView.dataSource = self
tableViewController.tableView.delegate = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parent.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let string = self.parent.data[indexPath.row]
let view = parent.viewBuilder(string)
let hostingController = UIHostingController(rootView: view)
let cell = SearchableListCell(withContent: hostingController, reuseIdentifier: "cell")
// Tried it with and without this line:
tableViewController.addChild(hostingController)
return cell
}
}
}
当我运行此命令时,例如使用此预览设置:
#if DEBUG
struct SearchableList_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
SearchableList(data: ["Berlin", "Dresden", "Leipzig", "Hamburg"]) { string in
NavigationLink(destination: Text(string)) { Text(string) }
}
.navigationBarTitle("Cities")
}
}
}
#endif
我只看到一个 TableView,其中有 4 个显然是空的单元格。在 View 层次结构调试器中,我可以看到,每个单元格确实都有 NavigationLink,其中的文本作为 subview ,只是不可见。因此我认为,这与将 UIHostingController 添加为 UITableViewController 的子级有关,但我只是不知道应该在哪里添加它。
目前有办法做到这一点吗?
最佳答案
我在尝试做同样的事情时发现了这一点,这对我有用。
对我来说,这是子类化和隐藏导航栏所必需的
import UIKit
import SwiftUI
/// SwiftUI UIHostingController adds a navbar for some reason so we must disable it
class ControlledNavigationHostingController<Content>: UIHostingController<AnyView> where Content: View {
public init(rootView: Content) {
super.init(rootView: AnyView(rootView.navigationBarHidden(true)))
}
@objc dynamic required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = true
}
}
这是要使用的主要对象
/// This UITableViewCell wrapper allows you to have a SwiftUI View in your UITableView
class HostingTableViewCell<Content: View>: UITableViewCell {
/// This holds the SwiftUI View being displayed in this UITableViewCell wrapper
private weak var swiftUIContainer: ControlledNavigationHostingController<Content>?
/// Put the SwiftUI View into the contentView of the UITableViewCell, or recycle an exisiting instance and add the new SwiftUIView
///
/// - Parameter view: The SwiftUI View to be used as a UITableViewCell
/// - Parameter parent: The nearest UIViewController to be parent of the UIHostController displaying the SwiftUI View
/// - Warning: May be unpredictable on the Simulator
func host(_ view: Content, parent: UIViewController) {
if let container = swiftUIContainer {
// Recycle this view
container.rootView = AnyView(view)
container.view.layoutIfNeeded()
} else {
// Create a new UIHostController to display a SwiftUI View
let swiftUICellViewController = ControlledNavigationHostingController(rootView: view)
swiftUIContainer = swiftUICellViewController
// Setup the View as the contentView of the UITableViewCell
swiftUICellViewController.view.backgroundColor = .clear
// Add the View to the hierarchy to be displayed
parent.addChild(swiftUICellViewController)
contentView.addSubview(swiftUICellViewController.view)
swiftUICellViewController.view.translatesAutoresizingMaskIntoConstraints = false
if let view = swiftUICellViewController.view {
contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
}
swiftUICellViewController.didMove(toParent: parent)
swiftUICellViewController.view.layoutIfNeeded()
}
}
}
像这样注册:
tableView.register(HostingTableViewCell<YourSwiftUIView>.self, forCellReuseIdentifier: "WhateverIDYouWant")
然后像这样使用:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID", for: indexPath) as? HostingTableViewCell<SomeSwiftUIView> else {
print("Error: Could Not Dequeue HostingTableViewCell<SomeSwiftUIView>")
return UITableViewCell()
}
cell.host(SomeSwiftUIView(), parent: self)
关于swift - 将 SwiftUI View 添加到 UITableViewCell contentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57627641/
当我将 UIView 放入 Storyboard中的 UITableViewCell 的 contentView 时,无法使它们具有相同的宽度。为什么是这样?我怎样才能使它们具有相同的宽度? subv
我正在创建一个使用 SAML 和 OAUTH2 的登录名。要获取登录 token ,我需要打开一个 Web View ,用户可以在其中输入他的登录凭据并推送登录。当他完成此操作后,它会重定向到带有 S
我正面临 contentView 高度限制的问题。所有标签都有动态高度,即没有固定高度。我在某处读过设置高度等于 ScrollView 高度。 当前 ViewController 的高度为 870px
A(当新创建单元格时): - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexP
我目前正在编写一个包含按钮和绘图的应用程序。我想做的是将两者合并到一个屏幕上。 setContentView(R.layout.activity_login_page); setContentView
使用卡片创建自定义内容 View 的最佳方式是什么?现在我这样做: fileprivate var contentView: View! fileprivate func prepar
我已经对 UITableViewCell 进行了子类化显示UIImage和两个UILabel subview 。在 TableView 的 View Controller 中,在方法 cellForR
我想禁用 Activity 中的用户交互,以便用户无法触摸 Activity 内容 View 内的 View / subview 。有什么办法可以实现吗? 最佳答案 您可以像这样在 Activity
我是 SwiftUI 的新手。试图推进我的计时器应用程序,我被卡住了。 在我的 ContentView 中,我有两个 ObservedObject,一个是管理计时器功能的 Timer 对象,另一个是存
在我正在开发的应用程序中,有一个多步骤注册,4 个步骤: 为了完成它,我想有一个页面来托管注册步骤的内容 View ,当它通过验证要求时,我将其删除并注入(inject)下一个内容 View 。 这是
我想为 contentView 设置填充从自定义内部 UICollectionViewCell所以它比细胞本身小。 我尝试通过为 contentView 设置 anchor 来做到这一点但这个 Vie
我有一个android项目作为库,这个库包含一个主屏幕。新应用程序需要这个主屏幕(以及库中的其他所有内容),但还需要对所有图形进行更改,并添加一些新按钮,我该怎么做? 我不想编辑库。我可以让新应用程序
我单独创建了一个 contentview,这样我就可以在不同的 ContentPage 中重用它。 这是我的 ContentView.XAML
我有一个 ScrollView,在其中更改其 contentSize (具体高度)。当我这样做时,附加到 ScrollView 的 ContentView 的大小不会相应改变。 ContentView
我有一个简单的 Nativescript 应用程序 - 只有 1 个组件显示标签“Hello World”。在安卓模拟器上运行良好。但是当我尝试在 IOS 设备上预览它时,它给了我这个错误 - 由于未
我正在使用 Grouped UITableView 并添加 UITableViewCells 两个 UILabel,并将它们添加到单元格的 contentView。我还在我的应用程序中在此 UITab
当我在表格 View 上选择一个单元格时,默认情况下它会将其 ContentView 背景颜色更改为浅灰色,我试图覆盖它。 我正在使用以下代码从选区中删除此灰色。但它正在删除该单元格内所有内容(UIV
我有一个带有背景图像的 ViewController,它作为 TableVC 作为它的 subview 。我希望 TableView 是透明的并且已经实现了,tableviewcell 也是透明的。但
我正在尝试以编程方式创建约束,以将这个粉红色的 UIView 置于 UITableViewCell 的中心。但是,当我添加约束时,它们不适用,并且我在控制台中收到一条消息,指出某些 NSAutores
我正在创建 ContentView 的子类 UserPostView 以用作 ListView 中的数据模板。我通过创建 UserPostView 的新实例将项目动态添加到 ListView 的 It
我是一名优秀的程序员,十分优秀!