Skip to content

Commit f88f9ca

Browse files
authored
Merge pull request #1 from cybozu/fix-navigation-for-uikit
Fixed a screen transition bug in navigation.
2 parents 7fa1290 + 10a0320 commit f88f9ca

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

Sources/LicenseList/LegacyLicenseView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct LegacyLicenseView: View {
2828
}
2929
}
3030
}
31+
.frame(maxWidth: .infinity, alignment: .leading)
3132
.padding()
3233
}
3334
.onAppear {

Sources/LicenseList/LicenseListView.swift

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import SwiftUI
99

1010
public struct LicenseListView: View {
1111
let libraries: [Library]
12+
let useUINavigationController: Bool
1213

13-
public init(fileURL: URL) {
14+
public init(fileURL: URL, useUINavigationController: Bool = false) {
15+
self.useUINavigationController = useUINavigationController
1416
guard let data = try? Data(contentsOf: fileURL),
1517
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
1618
let dict = plist as? [String: Any] else {
@@ -37,17 +39,49 @@ public struct LicenseListView: View {
3739
public var body: some View {
3840
List {
3941
ForEach(libraries, id: \.name) { library in
40-
if #available(iOS 15, *) {
41-
NavigationLink(destination: LicenseView(library: library)) {
42-
Text(library.name)
43-
}
42+
if useUINavigationController {
43+
libraryButton(library)
4444
} else {
45-
NavigationLink(destination: LegacyLicenseView(library: library)) {
46-
Text(library.name)
47-
}
45+
libraryNavigationLink(library)
4846
}
4947
}
5048
}
5149
.listStyleInsetGroupedIfPossible()
5250
}
51+
52+
var navigationController: UINavigationController? {
53+
guard let scene = UIApplication.shared.connectedScenes.first,
54+
let sceneDelegate = scene as? UIWindowScene,
55+
let rootVC = sceneDelegate.windows.first?.rootViewController,
56+
let navigationController = rootVC as? UINavigationController
57+
else { return nil }
58+
return navigationController
59+
}
60+
61+
func libraryButton(_ library: Library) -> some View {
62+
return Button(library.name) {
63+
let view: AnyView
64+
if #available(iOS 15, *) {
65+
view = AnyView(LicenseView(library: library))
66+
} else {
67+
view = AnyView(LegacyLicenseView(library: library))
68+
}
69+
let hostingController = UIHostingController(rootView: view)
70+
hostingController.title = library.name
71+
navigationController?.pushViewController(hostingController, animated: true)
72+
}
73+
.foregroundColor(.primary)
74+
}
75+
76+
func libraryNavigationLink(_ library: Library) -> some View {
77+
if #available(iOS 15, *) {
78+
return AnyView(NavigationLink(destination: LicenseView(library: library)) {
79+
Text(library.name)
80+
})
81+
} else {
82+
return AnyView(NavigationLink(destination: LegacyLicenseView(library: library)) {
83+
Text(library.name)
84+
})
85+
}
86+
}
5387
}

Sources/LicenseList/LicenseListViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class LicenseListViewController: UIViewController {
2323
public override func viewDidLoad() {
2424
super.viewDidLoad()
2525

26-
let licenseListView = LicenseListView(fileURL: fileURL)
26+
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true)
2727
let vc = UIHostingController(rootView: licenseListView)
2828
self.addChild(vc)
2929
self.view.addSubview(vc.view)

0 commit comments

Comments
 (0)