是否转到从SwiftUI到UIKit的另一个视图?
我使用是否转到从SwiftUI到UIKit的另一个视图?,swift,swiftui,uikit,Swift,Swiftui,Uikit,我使用uihostingcontroller在UIKit视图中加载SwiftUI视图 在我的SwiftUI视图中,我创建了一些水平滚动视图,其中包含一些内容 我需要能够单击/点击这些元素并转到我的UIKit中的另一个视图 这可能吗 我发现了这一点,但这显示将UIKit“重新加载”到SwiftUI视图中,这不是我想要做的,我认为这不是正确的方法: 这是我的SwiftUI代码: import SwiftUI struct videosContentView: View { var bod
uihostingcontroller
在UIKit视图中加载SwiftUI视图
在我的SwiftUI视图中,我创建了一些水平滚动视图,其中包含一些内容
我需要能够单击/点击这些元素并转到我的UIKit中的另一个视图
这可能吗
我发现了这一点,但这显示将UIKit“重新加载”到SwiftUI视图中,这不是我想要做的,我认为这不是正确的方法:
这是我的SwiftUI代码:
import SwiftUI
struct videosContentView: View {
var body: some View {
ScrollView{
ForEach(0..<2) {_ in
Section(header: Text("Important tasks")) {
VStack{
ScrollView(.horizontal){
HStack(spacing: 20) {
ForEach(0..<10) {
Text("Item \($0)")
.font(.headline)
.frame(width: 160, height: 200)
.background(Color.gray)
/*.padding()*/
.addBorder(Color.white, width: 1, cornerRadius: 10)
/*.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(Color.blue, lineWidth: 4)
)*/
}
}
}
}
}
}
}
}
}
struct videosContentView_Previews: PreviewProvider {
static var previews: some View {
videosContentView()
}
}
extension View {
public func addBorder<S>(_ content: S, width: CGFloat = 1, cornerRadius: CGFloat) -> some View where S : ShapeStyle {
let roundedRect = RoundedRectangle(cornerRadius: cornerRadius)
return clipShape(roundedRect)
.overlay(roundedRect.strokeBorder(content, lineWidth: width))
}
}
struct YourSwiftUIView:View{
@状态变量push=false
var body:一些观点{
如果推{
YourUIViewController()
}否则{
//你的内容
按钮(操作:{
withAnimation(){
push.toggle()
}
}) {
文本(“解雇我”)
.font(.headline)
.框架(宽:160,高:200)
.背景(颜色.灰色)
}
}
}
}
结构YourUIViewController:UIViewControllerRepresentable{
typealias UIViewControllerType=UIViewController
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->UIViewController{
让您的UIViewController=UIViewController()//您的UIViewController
返回UIViewController
}
func updateUIViewController(uViewController:uiViewController,上下文:UIViewControllerRepresentableContext){
}
}
这将从swiftuiview更改为UIViewController。您的另一个视图是UIViewController或其他什么?@RajaKishan,是的,它是另一个UIViewController。然后首先找到最顶端的控制器并推送视图控制器,或者您可以使用完成块。@RajaKishan,请查看我的编辑。如何从SwiftUI视图推送到另一个视图控制器?添加断点并检查self.navigationController是否为nill(如果为nill),然后首先将导航控制器嵌入到宿主控制器。我遇到以下错误:
无法在范围中找到“rootview”
根视图是您的UIVIewControllerOkay,因此,当我导航到另一个UIVIewController时,我会松开选项卡栏和导航栏。那么,您应该将UIVIewController包装在UIViewControllerRepresentable中,并通过.fullScreenCover将其推入。我会更新我的答案对不起,当我运行此代码时,我会得到一个盲动画,并且视图不会更改!
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("home") as home
self.navigationController.pushViewController(secondViewController, animated: true)
}) {
Text("Dismiss me")
.font(.headline)
.frame(width: 160, height: 200)
.background(Color.gray)
.addBorder(Color.white, width: 1, cornerRadius: 10)
}
struct YourSwiftUIView: View {
@State var push = false
var body: some View {
if push {
YourUIViewController()
}else {
//your content
Button(action: {
withAnimation() {
push.toggle()
}
}) {
Text("Dismiss me")
.font(.headline)
.frame(width: 160, height: 200)
.background(Color.gray)
}
}
}
}
struct YourUIViewController: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<YourUIViewController>) -> UIViewController {
let yourUIViewController = UIViewController() //your UIViewController
return yourUIViewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<YourUIViewController>) {
}
}