I use uihostingcontroller
to load a SwiftUI view in a UIKit View.
in my SwiftUI view, I create some horizontal ScrollViews with some stuff in them.
I need to be able to click/tap on these elements and go to another view in my UIKit.
Is this possible?
I found this but this shows to "reload" the UIKit into the SwiftUI view which is not what I want to do and I don't think this is the correct way of doing this anyway:
Is there any way to change view from swiftUI to UIKit?
This is my SwiftUI code:
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))
}
}
EDIT:
Based on suggestion in the comments, I tried this but this doesn't work:
Button(action: {
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)
}
question from:
https://stackoverflow.com/questions/65945162/going-to-another-view-from-swiftui-to-uikit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…