1

在我的应用程序中,我正在使用这样的 UISplitViewController:

let splitViewController = UISplitViewController()
splitViewController.preferredDisplayMode = .oneBesideSecondary
splitViewController.viewControllers = [
    UINavigationController(rootViewController: CalendarViewController()),
    DetailViewController()
]

结果:

在此处输入图像描述

但是当我将样式设置为doubleColumn这样时:

let splitViewController = UISplitViewController(style: .doubleColumn)

结果如下所示:

在此处输入图像描述

我不明白为什么现在 CalendarViewController 比主视图更宽。我想使用侧边栏,以便用户可以显示和隐藏日历。

如何修复此显示错误以使 CalendarViewController 具有与主视图相同的宽度?

4

1 回答 1

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
于 2021-10-27T21:49:54.753 回答