- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过传递 SwiftUI 将 SwiftUI View 用作子 UIView(在我的应用程序中位于 UIViewController 内)的内容。但是,SwiftUI View 一旦嵌入到 UIView 中就不会响应状态更改。
我在下面创建了有问题的代码的简化版本。
当点击嵌入在 EmbedSwiftUIView 中的 TextView 时,顶部 VStack 的外部 TextView 会按预期更新,但嵌入在 EmbedSwiftUIView 中的 TextView 不会更新其状态。
struct ProblemView: View {
@State var count = 0
var body: some View {
VStack {
Text("Count is: \(self.count)")
EmbedSwiftUIView {
Text("Tap to increase count: \(self.count)")
.onTapGesture {
self.count = self.count + 1
}
}
}
}
}
struct EmbedSwiftUIView<Content:View> : UIViewRepresentable {
var content: () -> Content
func makeUIView(context: UIViewRepresentableContext<EmbedSwiftUIView<Content>>) -> UIView {
let host = UIHostingController(rootView: content())
return host.view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<EmbedSwiftUIView<Content>>) {
}
}
最佳答案
更新 updateUIView
中的 View 或 View Controller 或 updateUIViewController
功能。在这种情况下,使用 UIViewControllerRepresentable
更容易。
struct EmbedSwiftUIView<Content: View> : UIViewControllerRepresentable {
var content: () -> Content
func makeUIViewController(context: Context) -> UIHostingController<Content> {
UIHostingController(rootView: content())
}
func updateUIViewController(_ host: UIHostingController<Content>, context: Context) {
host.rootView = content() // Update content
}
}
关于ios - SwiftUI View 在嵌入 UIView 时不更新其状态(使用 UIHostingController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60540825/
我有一个自定义 UIViewControllerRepresentable (布局相关代码如下所示)。这试图复制原生 SwiftUI ScrollView , 除了它从顶部以外的底部滚动。 查看层次结
我想从 UIViewController 加载一个基于 SwiftUI 的 View ,该 View 读取包本地的 json。仅 swiftUI 项目中的代码和绑定(bind)工作正常,当我利用 UI
我需要能够查看我的 SwiftUI View 以及呈现它们的 UIHosting Controller ,以便看到它们下面的背景图像。下面的代码仍然在我的 SwiftUI View 下显示一个白色矩形
我正在尝试将 SwiftUI 集成到我的项目中,我目前正在使用一个 Storyboard,该 Storyboard是通过我的应用委托(delegate)使用以下代码启动的: _rootNavigiat
我有一个 SwiftUI View ,我使用 UIHostingController 将其嵌入到现有的 UIViewController 中。 SwiftUI View 很简单,实际上我可以将其简化为
作为一名长期的 Obj-C 开发者(从 iPhone OS 2 开始),我认为 SwiftUI 是使用 Swift 的插入力! ¯\(ツ)/¯因此,我仍在努力理解一种语言为何在代码中如此类型模糊,而在
为什么 SwiftUI 的发行版构建会失败? 我将 SwiftUI 添加到我的 iOS 10+ 项目中,并使用 @available(iOS 13.0, *) 在本地构建它前缀,但是当我尝试构建用于分
我有一个基本的 SwiftUI 组件。 struct PopupModal: View { var body: some View { Text("Hello, World!"
我已将我的登录 View Controller 重写为 SwiftUI View . SignInView包裹在 UIHostingController 中子类 ( final class SignI
我有一个 swift UIHostingController,它呈现一个 SwiftUI。我在 View 中调用了一个函数,它构建良好但没有创建预期的输出。 class LoginView: UIHo
将 UIWindow.rootView 转换为托管 Controller ,例如: window?.rootViewController as? UIHostingController 工作正常,除非
假设我们有 UIWindow,UINavigationController 作为 rootViewController。 window = UIWindow() window!.makeKeyAndV
我正在关注 https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit我正在尝试将我的页面 View 附加到 UIHost
我正在使用 UIHostingController 将 ContentView 嵌入到 ViewController 中。当按下“更改名称”按钮时,我想更改 ContentView 的 name 的名
如何在UIViewController之间传递数据和 struct ContentView ? 我试过 ObservableObject但我无法获得最新的数据。 最佳答案 从 UIViewContro
我有一个相当广泛的项目,我从 UIKit 开始,现在我决定使用 SwiftUI 制作一些简单的表单页面,但我需要在 SwiftUI 中制作一个按钮来关闭当前 View ,该 View 显示如下代码:
我正在尝试为需要显示日期的 View 实现良好的过渡。我为 View 提供了一个 ID,以便 SwiftUI 知道它是一个新标签并通过转换为其设置动画。这是没有格式化程序和样式的压缩版本,并且具有较长
对于新的 SwiftUI iOS 应用程序,我在 SceneDelegate 中执行以下操作 if let windowScene = scene as? UIWindowScene { le
我想通过传递 SwiftUI 将 SwiftUI View 用作子 UIView(在我的应用程序中位于 UIViewController 内)的内容。但是,SwiftUI View 一旦嵌入到 UIV
我正在学习 Swift 和 SwiftUI 作为没有 UIKit 背景的业余爱好,所以我不确定这目前是否可行。我真的很想在 SwiftUI 中使用 UIKit 的上下文菜单(例如,实现子菜单、操作属性
我是一名优秀的程序员,十分优秀!