diff --git a/host/apple/Sources/ExactKit/LocalInsets.swift b/host/apple/Sources/ExactKit/LocalInsets.swift index 8bda8d57f..3a98160d2 100644 --- a/host/apple/Sources/ExactKit/LocalInsets.swift +++ b/host/apple/Sources/ExactKit/LocalInsets.swift @@ -11,6 +11,9 @@ import CoreGraphics package struct LocalInsetsReport { /// Top, right, bottom, left, in points. package var wanted: [UInt32?: [CGFloat]] = [:] + /// The owners whose insets are another's until their container gives them their own: a route UIKit has yet to + /// place, which holds those of the route its stack shows. + package var borrowed: Set = [] private var delivered: [UInt32?: [CGFloat]] = [:] package init() {} diff --git a/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift b/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift index d767e0a07..d9b1d9beb 100644 --- a/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift @@ -449,6 +449,7 @@ extension NavigationHost { let columns = nav.flatMap { n in (split?.detail === n ? split : nil) ?? tabSplits.values.first { $0.detail === n } } let detail = columns != nil for (index, c) in routes.enumerated() { + c.arriving = nav // The bar is each route's (LLP 1115.000 D5.2, D5.3): its header, // the system Back alone on a pushed route with none of its own // (no header, a hidden one, one that is not a bar's), or no bar diff --git a/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift b/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift index 342385e90..4fafac16b 100644 --- a/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift @@ -686,15 +686,25 @@ extension NavigationHost { /// take the whole view, where `env()` is 0 but the screen's insets are not. /// Whether any changed. func reportLocalInsets() -> Bool { - var wanted: [UInt32?: [CGFloat]] = [:] + var wanted: [UInt32?: [CGFloat]] = [:], borrowed: Set = [] for c in controllers.values where presenter.views[c.node.id] === c.node { - let last = localInsets.wanted[c.node.id] + // What UIKit last gave the route: nothing while it holds insets it borrowed arriving (`arrivingInsets`) + let last = localInsets.borrowed.contains(c.node.id) ? nil : localInsets.wanted[c.node.id] let stack = stackTransition(for: c) let moving = stack != nil || c.navigationController?.transitionCoordinator != nil let presenting = c.inPresentation && stack == nil let view = c.surface guard view.window != nil, c.node.window != nil, !c.safeAreaNeedsLayout, last == nil || !moving || c.appearing || presenting else { - wanted[c.node.id] = last + // A route written to a stack UIKit has yet to place it in borrows the insets of the route that stack + // shows, the safe area UIKit gives it there (`RouteController.arriving`): its content lays out where + // it will rest, and UIKit's first measure once it places the route changes nothing + if last == nil, view.window == nil, let insets = arrivingInsets(c) { + wanted[c.node.id] = insets + borrowed.insert(c.node.id) + } else { + wanted[c.node.id] = localInsets.wanted[c.node.id] + if localInsets.borrowed.contains(c.node.id) { borrowed.insert(c.node.id) } + } continue } let box = c.node.convert(c.node.bounds, to: view) @@ -719,10 +729,16 @@ extension NavigationHost { let safe = ExactView.agentSize == nil ? view.safeAreaInsets : .zero wanted[nil] = [safe.top, safe.right, safe.bottom, safe.left] } - defer { localInsets.wanted = wanted } + defer { localInsets.wanted = wanted; localInsets.borrowed = borrowed } return localInsets.wanted != wanted } + /// The local insets of the route shown in the stack `route` is written to (`RouteController.arriving`). + private func arrivingInsets(_ route: RouteController) -> [CGFloat]? { + guard let shown = route.arriving?.stackRoutes.last as? RouteController, shown !== route else { return nil } + return localInsets.wanted[shown.node.id] + } + func padsTop(_ route: RouteController) -> Bool { guard case .edges(let edges)? = covers[route.node.id] else { return false } return edges.top > 0.5 && route.collapseScroll == nil diff --git a/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift b/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift index 2039b3ae6..0a080a7e9 100644 --- a/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift @@ -574,6 +574,10 @@ final class RouteController: UIViewController, UIScrollViewDelegate { /// for that Exact has not written yet. var held = false weak var lastStack: UINavigationController? + /// The stack the router last gave the route (`NavigationHost.prepareRoutes`), which UIKit places it in when it is + /// written there: until then the route borrows the local insets of the route that stack shows + /// (`reportLocalInsets`). + weak var arriving: UINavigationController? /// The route's node, left in place after a batch removed the route while /// UIKit still shows it (`NavigationHost.prepare`), until UIKit takes the