我正在实现 iOS 14 (iPadOS 14) 侧边栏(带有 TripleColumn 的 UISplitViewController)并且有奇怪的“侧边栏切换图标”行为。在 iOS 13 中,我使用带有一些拆分视图和表格视图的标签栏,所以我需要三列而不是双列来工作。
并且有些选项卡只有一列(在 iOS 13 中,它是表格视图而不是拆分视图)。我将补充视图设置为 nil,并通过调用 iOS 14 中实现的“隐藏”方法来隐藏视图。(代码见下文):
自动显示左上角的“侧边栏切换图标”。单击切换图标后,侧边栏正确隐藏,但在我的辅助视图上创建了一个“后退按钮”(嵌入在 UINavigationController 中的 UITableViewController):
按下(单击)后退按钮没有响应。用户仍然可以从屏幕的左边缘滑动以使侧边栏重新出现,但“后退按钮”令人困惑。我的预期行为是,在侧边栏中选择切换图标后,在辅助视图中显示“侧边栏切换图标”而不是“后退按钮”。在辅助视图中按下“侧边栏切换图标”后,侧边栏会重新出现。
与 iOS 14 (iPadOS 14) 中的照片应用程序一样,显示的是切换按钮而不是返回按钮。单击切换图标将使侧边栏再次显示。(但它是一个双列拆分视图)
我的代码:
SceneDelegate.swift:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
if #available(iOS 14.0, *) {
let main = UIStoryboard(name: "Main", bundle: nil)
let splitViewController = UISplitViewController(style: .tripleColumn)
splitViewController.preferredDisplayMode = .twoBesideSecondary
splitViewController.preferredSplitBehavior = .tile
splitViewController.setViewController(SideBarViewController(), for: .primary)
// fall back for compact screen
splitViewController.setViewController(main.instantiateInitialViewController(), for: .compact)
window.rootViewController = splitViewController
self.window = window
window.makeKeyAndVisible()
}
}
}
侧边栏视图控制器:
// if the first tab (dashboard) was selected
private func selectDashboardTab() {
if #available(iOS 14.0, *) {
let dashboardVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DashboardTab") as? UINavigationController
splitViewController?.preferredPrimaryColumnWidth = 250.0
splitViewController?.preferredDisplayMode = .twoBesideSecondary
splitViewController?.preferredSplitBehavior = .tile
splitViewController?.setViewController(dashboardVC, for: .secondary)
splitViewController?.setViewController(nil, for: .supplementary)
splitViewController?.hide(.supplementary)
}
}