![](/img/trans.png)
[英]TapGesture added to UIView+SomeCategory. How can I use this UIView that is added to my UIViewController?
[英]How can I use UIViewController and UIView in SwiftUI?
我尝试将自定义UIViewController集成到Swift应用程序中,但是无法将其与诸如Text()的SwiftUI元素集成。 我收到此错误:函数声明了一个不透明的返回类型,但是在其主体中没有用于从其推断基础类型的return语句。 我应该使用UIView代替UIViewController吗?
UIView
和UIViewController
可以通过各自的类UIViewRepresentable
和UIViewControllerRepresentable
在SwiftUI中实现。
Apple的SwiftUI教程介绍了此方法,以及将UIKit集成到SwiftUI的其他方法。
SwiftUI教程: https : //developer.apple.com/tutorials/swiftui/tutorials
使用UIViewRepresentable将UIView放入SwiftUI: https : //developer.apple.com/tutorials/swiftui/creating-and-combining-views ^请参阅第5节,该节使用MKMapView作为UIView
与UIKit交互: https : //developer.apple.com/tutorials/swiftui/interface-with-uikit
很抱歉我的答案中没有代码,我现在是AFK。 但是,最好的学习方法是自己找出并解决问题。 希望这可以帮助!
更新
如果要将SwiftUI集成到现有的UIKit应用程序中,请考虑使用UIHostingController。 它的工作原理如下:
UIHostingController(rootView: MySwiftUIStruct())
https://developer.apple.com/documentation/swiftui/uihostingcontroller
它允许您将SwiftUI内容放置在UIKit应用程序中,就像将UIView或UIViewController放置在其中一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.