From 23c272f1ee13b220b5e53769cf44b3fadfdcc097 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Sat, 5 Dec 2026 13:26:55 +0800 Subject: [PATCH] App: a page without buttons scrolls to the bottom of the screen The bar around the buttons kept its padding with no buttons in it (the pages while access is obtained), so the page stopped short of the bottom of the screen. Without buttons the bar now takes no room; the page's own padding is under its last line. The layout check gives each size its device's status bar and home indicator, not those of the simulator it runs in. Assisted-by: LLM --- App/LayoutCheck.swift | 23 +++++++++++++++++++++++ App/RootViewController.swift | 15 ++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/App/LayoutCheck.swift b/App/LayoutCheck.swift index 7381ef5..17e8d7c 100644 --- a/App/LayoutCheck.swift +++ b/App/LayoutCheck.swift @@ -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. @@ -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 { diff --git a/App/RootViewController.swift b/App/RootViewController.swift index 9dc6ed4..387c4cf 100644 --- a/App/RootViewController.swift +++ b/App/RootViewController.swift @@ -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, ]) @@ -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) { @@ -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 }