我使用
uihostingcontroller
在 UIKit 视图中加载 SwiftUI 视图。
在我的 SwiftUI 视图中,我创建了一些水平 ScrollView,其中包含一些内容。
我需要能够单击/点击这些元素并转到 UIKit 中的另一个视图。
这可能吗?
我发现了这个,但这显示将 UIKit“重新加载”到 SwiftUI 视图中,这不是我想要做的,而且我认为这不是正确的方法:
有什么方法可以将视图从 swiftUI 更改为 UIKit 吗?
这是我的 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))
}
}
编辑:
根据评论中的建议,我尝试了这个,但这不起作用:
按钮(操作:{
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)
}
要将
UIViewController
桥接到 SwiftUI,您必须使用 UIViewControllerRepresentable
:
struct SwiftUIView: View {
@State var push = false
var body: some View {
if push {
ViewControllerContent()
}else {
//your content
Button(action: {
withAnimation() {
push.toggle()
}
}) {
Text("Dismiss me")
.font(.headline)
.frame(width: 160, height: 200)
.background(Color.gray)
}
}
}
}
struct ViewControllerContent: UIViewControllerRepresentable {
//replace UIViewController with your actual controller.
func makeUIViewController(context: Context) -> UIViewController {
let controller = UIViewController()
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//Use this function to update your controller
}
}
这是从 UIKit 到 SwiftUI 的转换示例,反之亦然:
UIKit 到 SwiftUI:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "UIKit View Controller"
}
}
struct ContentView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
typealias UIViewControllerType = UIViewController
}
struct ContentViewExample: View {
var body: some View {
ZStack{
ContentView()
}
}
}
SwiftUI 到 UIKit:
struct ContentView: View {
var body: some View {
ZStack{
Text("SwiftUI View")
}
}
}
class ViewController: UIViewController {
private var hostingController: UIHostingController<ContentView>!
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func setupViews() {
hostingController = UIHostingController(rootView: ContentView())
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingController.view)
NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}