[英]How to add Context Menu into NavigationLink using SwiftUI?
[英]How to add a title to a UIViewController that is pushed via a SwiftUI NavigationLink?
我正在通过NavigationLink
从 SwiftUI 视图将符合UIViewControllerRepresentable
的UIViewController
推送到导航堆栈上,并且我想为 ViewController 设置标题。
但是,我无法获得一个空标题。 我怀疑这是一个 SwiftUI 错误,但希望我可能遗漏了一些东西。
以下是我尝试过的行不通。
title = "Title"
navigationItem.title = "Title"
这是重现问题的代码
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink {
ViewControllerRepresentable()
} label: {
Text("Go to ViewController")
}
}
.navigationTitle(Text("SwiftUI View"))
}
}
}
struct ViewControllerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = ViewController
func makeUIViewController(context: Context) -> ViewController {
ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "title"
title = "title"
}
}
尝试在 SwiftUI 中设置标题:
NavigationLink {
ViewControllerRepresentable()
.navigationTitle("My UIViewController")
} label: {
Text("Go to ViewController")
}
然后您可以从您的视图 controller 中删除这些行,除非您也打算在 UIKit 中使用它(在这种情况下,只需navigationItem.title
就足够了):
navigationItem.title = "title"
title = "title"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.