Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions App/LayoutCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ enum LayoutCheck {
return upright + upright.map { CGSize(width: $0.height, height: $0.width) }
}()

/// The status bar and home indicator of each size's device: the iPod touch, iPhone 8
/// and the iPads with a Home button have a 20-point status bar (none on a phone on its
/// side); the iPhone XR has its notch and home indicator, the iPad Pro its rounded corners.
static func insets(_ size: CGSize) -> UIEdgeInsets {
let upright = size.height > size.width
switch (Int(min(size.width, size.height)), Int(max(size.width, size.height))) {
case (414, 896): return upright ? UIEdgeInsets(top: 48, left: 0, bottom: 34, right: 0) : UIEdgeInsets(top: 0, left: 48, bottom: 21, right: 48)
case (1024, 1366): return UIEdgeInsets(top: 24, left: 0, bottom: 20, right: 0)
case (768, _), (810, _): return UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
default: return upright ? UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) : .zero
}
}

/// The problems of one screen: `make` builds its controller for a size (it may
/// word the screen for the device of that size), laid out first in a tiny window,
/// as a window can be before it gets its size, then at each size in turn.
Expand All @@ -43,6 +56,16 @@ enum LayoutCheck {
window.layoutIfNeeded()
window.frame = CGRect(origin: .zero, size: size)
window.layoutIfNeeded()
// The insets of the device of that size, not of the simulator the check runs in.
// Measured on the screen and corrected, as the window may report its own late.
let want = insets(size)
for _ in 0..<3 where controller.view.safeAreaInsets != want {
let have = controller.view.safeAreaInsets, extra = host.additionalSafeAreaInsets
host.additionalSafeAreaInsets = UIEdgeInsets(top: extra.top + want.top - have.top, left: extra.left + want.left - have.left,
bottom: extra.bottom + want.bottom - have.bottom, right: extra.right + want.right - have.right)
window.layoutIfNeeded()
}
if controller.view.safeAreaInsets != want { problems.append("\(screen) at \(Int(size.width))x\(Int(size.height)): the check could not set the device's insets (\(controller.view.safeAreaInsets))") }
problems += check(controller.view, in: window).map { "\(screen) at \(Int(size.width))x\(Int(size.height)): \($0)" }
// ORCHARD_LAYOUT_SHOTS=DIR: a picture of the screen, upright, at 2x, for review.
if let dir = ProcessInfo.processInfo.environment["ORCHARD_LAYOUT_SHOTS"], size.height > size.width {
Expand Down
15 changes: 10 additions & 5 deletions App/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
footerBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
footerBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
footerBar.bottomAnchor.constraint(equalTo: view.bottomAnchor),
footer.topAnchor.constraint(equalTo: footerBar.topAnchor, constant: 12),
footer.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: -16),
footerFilled[0], footerFilled[1],
footer.centerXAnchor.constraint(equalTo: footerBar.centerXAnchor),
footer.widthAnchor.constraint(lessThanOrEqualToConstant: metrics.contentWidth), footerWidth, footerCap,
])
Expand All @@ -110,8 +109,12 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
}
private var sideConstraints: [NSLayoutConstraint] = []
private var headerTop: NSLayoutConstraint?
/// An empty footer is no height, not one iOS picks (LayoutCheck.swift).
private lazy var footerEmpty = footer.heightAnchor.constraint(equalToConstant: 0)
/// The bar around the buttons; without buttons it takes no room at all, and the page
/// scrolls to the bottom of the screen with its own padding under it.
private lazy var footerFilled = [footer.topAnchor.constraint(equalTo: footerBar.topAnchor, constant: 12),
footer.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16)]
private lazy var footerEmpty = [footerBar.topAnchor.constraint(equalTo: view.bottomAnchor), footer.heightAnchor.constraint(equalToConstant: 0),
footer.topAnchor.constraint(equalTo: footerBar.topAnchor)]

/// A hairline under the header once the page scrolls beneath it.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
Expand Down Expand Up @@ -203,7 +206,9 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
footer.arrangedSubviews.forEach { $0.removeFromSuperview() }
for view in Pages(self, metrics).footer() { footer.addArrangedSubview(view) }
footerBar.isHidden = footer.arrangedSubviews.isEmpty
footerEmpty.isActive = footer.arrangedSubviews.isEmpty
let empty = footer.arrangedSubviews.isEmpty
NSLayoutConstraint.deactivate(empty ? footerFilled : footerEmpty)
NSLayoutConstraint.activate(empty ? footerEmpty : footerFilled)
footerKey = key
}

Expand Down
Loading