Fix room titles covering the toolbar buttons after cancelling a swipe back (#6386)

* Fix custom titles covering the toolbar after a cancelled swipe back

On iOS 26 and 27, cancelling the interactive pop gesture leaves a custom
navigation title view (e.g. the room header) at its full width, so it
covers the trailing toolbar buttons until the screen is left. UIKit
restores the title's container but never lays the title out inside it
again.

Work around it by observing both pop gestures and laying out the title
view's container again when the pop is cancelled. It's only applied on
iOS 26+ and marked with a TODO to remove once Apple fixes the bug.

* Make singletons a last resort in AGENTS.md

Add a Dependency Injection rule: use a shared singleton only as a last
resort.

* Apply the cancelled swipe back title fix only to room header screens

Review feedback: hooking every navigation stack does more introspection
than needed. The bug only shows up when RoomHeaderView is the title, so
move the workaround next to it and apply it to the 4 screens that use
it: room, thread, space and join room (knocked).

Checked on the simulator: with the fix removed, a cancelled swipe back
leaves the title too wide on the room, thread and join room screens;
with the fix applied, the room, thread, join room and space titles stay
truncated.

Re-record the SpaceScreen preview snapshots. Wrapping that screen in
any if/else changes how the snapshot test renders it, even when the
workaround itself doesn't run. The app isn't affected.
This commit is contained in:
Mauro
2026-10-27 11:34:46 +01:00
committed by GitHub
parent d129be0cf3
commit d82f014dfb
16 changed files with 90 additions and 20 deletions
+1
View File
@@ -278,6 +278,7 @@ Services = where product-level opinions live (Rust SDK stays spec-faithful).
- Need 2+ from it? Pass `userSession` down. Need 1? Pass that one direct.
- Receiver may store only pieces it need (e.g. `clientProxy`, `userSettings`).
- Never pass `userSession` plus value it already owns (e.g. `userSession` + `userSettings`).
- Singletons (`static let shared`) last resort.
---
@@ -9,6 +9,7 @@
import Combine
import Compound
import SwiftUI
import SwiftUIIntrospect
struct RoomHeaderView: View {
struct DMRecipientDetails {
@@ -126,6 +127,70 @@ private extension View {
}
}
extension View {
// TODO: Remove this workaround once Apple fixes the bug.
/// Works around an iOS 26+ bug where cancelling the interactive pop gesture leaves the `RoomHeaderView`
/// at its full width, covering the trailing toolbar items. Apply it to every screen using the header as its title.
@ViewBuilder
func fixesTitleViewOnCancelledPop() -> some View {
if #available(iOS 26, *) {
modifier(CancelledPopTitleViewFixModifier())
} else {
self
}
}
}
@available(iOS 26, *)
private struct CancelledPopTitleViewFixModifier: ViewModifier {
// Gesture recognizers don't retain their targets, so the view keeps the fix alive.
@State private var fix = CancelledPopTitleViewFix()
func body(content: Content) -> some View {
content
.introspect(.navigationStack, on: .supportedVersions, scope: .ancestor) { [fix] navigationController in
fix.observe(navigationController)
}
}
}
@available(iOS 26, *)
private final class CancelledPopTitleViewFix: NSObject {
private weak var navigationController: UINavigationController?
isolated deinit {
stopObserving()
}
func observe(_ navigationController: UINavigationController) {
guard navigationController !== self.navigationController else { return }
stopObserving()
self.navigationController = navigationController
navigationController.interactivePopGestureRecognizer?.addTarget(self, action: #selector(popGestureChanged))
navigationController.interactiveContentPopGestureRecognizer?.addTarget(self, action: #selector(popGestureChanged))
}
@objc private func popGestureChanged(_ gesture: UIGestureRecognizer) {
guard gesture.state == .began,
let navigationController,
let transitionCoordinator = navigationController.transitionCoordinator else {
return
}
transitionCoordinator.animate(alongsideTransition: nil) { context in
guard context.isCancelled else { return }
// UIKit restores the title view's container but never lays the title view out inside it again.
navigationController.navigationBar.topItem?.titleView?.superview?.setNeedsLayout()
}
}
private func stopObserving() {
navigationController?.interactivePopGestureRecognizer?.removeTarget(self, action: #selector(popGestureChanged))
navigationController?.interactiveContentPopGestureRecognizer?.removeTarget(self, action: #selector(popGestureChanged))
}
}
// MARK: - Previews
struct RoomHeaderView_Previews: PreviewProvider, TestablePreview {
@@ -41,6 +41,7 @@ struct JoinRoomScreen: View {
.background()
.backgroundStyle(.compound.bgCanvasDefault)
.toolbarRole(RoomHeaderView.toolbarRole)
.fixesTitleViewOnCancelledPop()
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.shouldScrollOnKeyboardDidShow(focus == .knockMessage, to: Focus.knockMessage)
@@ -119,6 +119,7 @@ struct RoomScreen: View {
}
.navigationBarBackButtonHidden(isMessageSelectionActive)
.toolbarRole(RoomHeaderView.toolbarRole)
.fixesTitleViewOnCancelledPop()
.navigationTitle(L10n.screenRoomTitle) // Hidden but used for back button text.
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
@@ -34,6 +34,7 @@ struct SpaceScreen: View {
.environment(\.editMode, .constant(context.viewState.editMode))
.background(Color.compound.bgCanvasDefault.ignoresSafeArea())
.toolbarRole(RoomHeaderView.toolbarRole)
.fixesTitleViewOnCancelledPop()
.navigationTitle(context.viewState.space.name)
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(isEditModeActive)
@@ -26,6 +26,7 @@ struct ThreadTimelineScreen: View {
TimelineView(timelineContext: timelineContext)
.background(.compound.bgCanvasDefault)
.toolbarRole(RoomHeaderView.toolbarRole)
.fixesTitleViewOnCancelledPop()
.navigationTitle(L10n.commonThread)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:85113d4366fe0a216b13f93d81b9cd0cb05c3b57f01c2448fdecae274208c3f9
size 136916
oid sha256:adc5894a3f81afad0d0aae4da68ec830dc04fa642c4c106ef2916bc0bb35accf
size 138773
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:635a9c95bb85a57b2bfa61a311003cb3c126d08c0e1312b563924a03dd429bf5
size 153028
oid sha256:2c3b66df7286ecfb96002b82b19b545bdf924f8363beaba882157ca11719610c
size 153534
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:aea34376a5f654c81e617afc9dde8db2b937dd6ad5c5325cf57595aaa5badae7
size 139039
oid sha256:ff7bc72fa95d4cea38b41795a891f7714dbbfab6d67b46766e0c6320d6ba13b5
size 139115
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f3588bb282fb61e1a23e7724dff228d5356085fff7365e39cd8479e88e9e900f
size 146835
oid sha256:6358e49c5b4d74e8d8ad2371c6b08056693f263bc7066d077c5c9f7b16281e54
size 143920
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8deb8cc0ccdc283a17e3d48fdb024426b21f51b793076f0732c69c877a38cbd9
size 92528
oid sha256:ec36e9f75c5967ce57ae49490605a76efbba765cfa59cf383bf3379bf72d5e9a
size 92159
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6d70a5c7a99ae93495b05fdd5f6edba02f063a55b08de9e1b04ac633f21e218f
size 105212
oid sha256:7170c60bfe684b34bccdc3f6c183f830312b2064ff0b5965f623944671519553
size 104896
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4d891d0e77b960bc225dd22e6949ad4948249057c587b93ff5af338d0eefb03c
size 235058
oid sha256:cd484d0701e7e574c406edd7c94dead0ac7986ab8a34f3bfa9c469edf494db22
size 235204
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0824909c9063d94e2b9fa212f25c1e6132cb3e73388fb7f0d85a035b3a4061f1
size 266916
oid sha256:a24d75b529762a00b3df4ccb3dca9d9ec1c88b4a8b5d1b031977ea1998e1662c
size 260942
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e9f79f0f119a77b1659aa33b8f002e0485cf79475f033073b5e4cd19ff861589
size 177370
oid sha256:8370211a447162279955b9c68fc31ee18e6d3bb014cde5b01ab574eda04672b7
size 176891
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e46c14eac78312f2062f40eb2d9a7833b3fafe3e18d6bd075804db613a05b5bb
size 202083
oid sha256:063206bb855eadc5586a007d4f55b2e114305e0009bec9a67c3dc0e2ea71d8df
size 201669