UISplitViewController - 双列样式不起作用
Posted
技术标签:
【中文标题】UISplitViewController - 双列样式不起作用【英文标题】:UISplitViewController - doubleColumn Style not working 【发布时间】:2021-09-12 10:18:25 【问题描述】:在我的应用程序中,我正在使用这样的 UISplitViewController:
let splitViewController = UISplitViewController()
splitViewController.preferredDisplayMode = .oneBesideSecondary
splitViewController.viewControllers = [
UINavigationController(rootViewController: CalendarViewController()),
DetailViewController()
]
结果:
但是当我像这样将样式设置为doubleColumn
时:
let splitViewController = UISplitViewController(style: .doubleColumn)
结果如下所示:
我不明白为什么现在 CalendarViewController 比主视图更宽。我想使用侧边栏,以便用户可以显示和隐藏日历。
如何解决此显示错误,以使 CalendarViewController 具有与主视图相同的宽度?
【问题讨论】:
【参考方案1】:这是 CalendarKit 库中的一个错误,已在以下提交中修复:Fix Layout Issue when using UISplitViewController
问题出在没有考虑safeArea
指南的布局代码中:
dayHeaderView.frame = CGRect(origin: CGPoint(x: 0, y: layoutMargins.top),
size: CGSize(width: bounds.width, height: headerHeight))
let timelinePagerHeight = bounds.height - dayHeaderView.frame.maxY
timelinePagerView.frame = CGRect(origin: CGPoint(x: 0, y: dayHeaderView.frame.maxY),
size: CGSize(width: bounds.width, height: timelinePagerHeight))
切换到 AutoLayout 后,问题就消失了:
dayHeaderView.translatesAutoresizingMaskIntoConstraints = false
timelinePagerView.translatesAutoresizingMaskIntoConstraints = false
dayHeaderView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor).isActive = true
dayHeaderView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor).isActive = true
dayHeaderView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor).isActive = true
let heightConstraint = dayHeaderView.heightAnchor.constraint(equalToConstant: headerHeight)
heightConstraint.priority = .defaultLow
heightConstraint.isActive = true
timelinePagerView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor).isActive = true
timelinePagerView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor).isActive = true
timelinePagerView.topAnchor.constraint(equalTo: dayHeaderView.bottomAnchor).isActive = true
timelinePagerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
【讨论】:
以上是关于UISplitViewController - 双列样式不起作用的主要内容,如果未能解决你的问题,请参考以下文章
UISplitViewController - 并排或纵向叠加
在 UIViewControllers 和 UISplitViewController 之间导航 [关闭]
将 UIToolBar 放在 UISplitViewController 上方?
UISplitViewController + UISearchController 显示搜索细节