SO 上还有另一篇关于这个问题的文章,但它是从前一段时间开始的,只涉及 UIKit,而不是 SwiftUI。我的 SFSafariViewController 工作完美,并且通过编程式 NavigationLink 使用它。
问题是“完成”按钮的可点击区域太小。我只能以一定的角度点击“完成”一词的“ne”。这很奇怪,我一直无法解决这个问题。其余按钮都可以完美使用。
如果我更改为通过工作表以模式方式使用它,“完成”按钮可以完美工作,但我需要通过导航链接使用它。
我只需要解决“完成”按钮的可点击区域的问题。
谢谢!
这是我的 SFSafariViewController:
import SwiftUI
import SafariServices
struct SafariView: UIViewControllerRepresentable {
@Environment(\.dismiss) var dismiss
let url: URL
func makeUIViewController(context: Context) -> SFSafariViewController {
let vc = SFSafariViewController(url: url)
vc.preferredControlTintColor = .tintColor
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ vc: SFSafariViewController, context: Context) {}
class Coordinator: NSObject, SFSafariViewControllerDelegate {
var dismissAction: DismissAction?
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismissAction?()
}
}
func makeCoordinator() -> Coordinator {
let coordinator = Coordinator()
coordinator.dismissAction = dismiss
return coordinator
}
}
这是我的观点:
import SwiftUI
struct HomeView: View {
@State private var isBtnActive: Bool = false
var body: some View {
NavigationLink(
destination: SafariView(url: URL(string: "https://www.example.com")!)
.navigationBarHidden(true),
isActive: $isBtnActive,
label: {
EmptyView()
}
)
.accessibilityHidden(true)
VStack(spacing: 0) {
Spacer()
Button("Show Website") {
isBtnActive = true
}
Spacer()
}
}
}
我在 SwiftUI 中找到了关于此主题的解决方案。
此代码将 SafariViewController 嵌入到 NavigationView 中,并且“完成”按钮正在工作。唯一不起作用的是向后滑动手势。
import SwiftUI
import SafariServices
struct SafariWebView: UIViewControllerRepresentable {
let url: URL
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> UINavigationController {
let safariVC = SFSafariViewController(url: url)
safariVC.delegate = context.coordinator
let navController = UINavigationController(rootViewController: safariVC)
navController.setNavigationBarHidden(true, animated: false)
return navController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
class Coordinator: NSObject, SFSafariViewControllerDelegate {
var parent: SafariWebView
init(_ parent: SafariWebView) {
self.parent = parent
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
parent.presentationMode.wrappedValue.dismiss()
}
}
}
struct AppLink: View {
var url: String
var body: some View {
SafariWebView(url: URL(string: url)!)
.ignoresSafeArea()
.navigationBarBackButtonHidden(true)
}
}
#Preview {
AppLink(url: "https://www.carlosveny.com")
}
然后您可以将其与 NavigationLink 一起使用:
NavigationStack {
NavigationLink("Link App", destination: AppLink(url: "https://www.carlosveny.com"))
}