diff --git a/Modules/Sources/WordPressUI/Views/AdaptiveTabBarController.swift b/Modules/Sources/WordPressUI/Views/AdaptiveTabBarController.swift index 7b48ff5f37d9..01c0537cc865 100644 --- a/Modules/Sources/WordPressUI/Views/AdaptiveTabBarController.swift +++ b/Modules/Sources/WordPressUI/Views/AdaptiveTabBarController.swift @@ -34,6 +34,14 @@ public final class AdaptiveTabBarController { public let segmentedControl = UISegmentedControl() + private lazy var segmentedControlItem: UIBarButtonItem = { + let button = UIBarButtonItem(customView: segmentedControl) + if #available(iOS 26.0, *) { + button.hidesSharedBackground = true + } + return button + }() + private weak var viewController: UIViewController? private var onSelectionChanged: ((Item) -> Void)? @@ -84,17 +92,21 @@ public final class AdaptiveTabBarController { private func refresh() { guard let viewController else { return } + let navigationItem = self.navigationItem ?? viewController.navigationItem + // Keep any items the screen added itself, such as a Done button. + let otherItems = (navigationItem.leftBarButtonItems ?? []).filter { $0 !== segmentedControlItem } + if viewController.traitCollection.horizontalSizeClass == .regular { filterBarContainer.removeFromSuperview() - (navigationItem ?? viewController.navigationItem).leftBarButtonItem = { - let button = UIBarButtonItem(customView: segmentedControl) - if #available(iOS 26.0, *) { - button.hidesSharedBackground = true - } - return button - }() + navigationItem.leftBarButtonItems = otherItems + [segmentedControlItem] + // A regular width doesn't mean the screen is the root of a split view column. On the + // iPhone Duo's inner screen it's pushed with the tab bar hidden, so it needs its back button. + navigationItem.leftItemsSupplementBackButton = true viewController.additionalSafeAreaInsets = .zero } else { + if navigationItem.leftBarButtonItems?.contains(segmentedControlItem) == true { + navigationItem.leftBarButtonItems = otherItems + } viewController.navigationItem.titleView = nil viewController.view.addSubview(filterBarContainer) filterBarContainer.pinEdges([.top, .horizontal]) diff --git a/Modules/Tests/WordPressUIUnitTests/AdaptiveTabBarControllerTests.swift b/Modules/Tests/WordPressUIUnitTests/AdaptiveTabBarControllerTests.swift new file mode 100644 index 000000000000..74c8788cf26d --- /dev/null +++ b/Modules/Tests/WordPressUIUnitTests/AdaptiveTabBarControllerTests.swift @@ -0,0 +1,66 @@ +import Testing +import UIKit +import WordPressUI + +@MainActor +struct AdaptiveTabBarControllerTests { + private let viewController = UIViewController() + private let doneButton = UIBarButtonItem(systemItem: .done) + private let controller = AdaptiveTabBarController() + + init() { + viewController.navigationItem.leftBarButtonItem = doneButton + } + + @Test func regularWidthKeepsTheBackButtonAndExistingItems() { + viewController.traitOverrides.horizontalSizeClass = .regular + controller.configure(Tab.allCases, in: viewController) { _ in } + + let items = viewController.navigationItem.leftBarButtonItems ?? [] + #expect(items.count == 2) + #expect(items.first === doneButton) + #expect(items.last?.customView === controller.segmentedControl) + #expect(viewController.navigationItem.leftItemsSupplementBackButton) + } + + @Test func compactWidthLeavesTheNavigationItemAlone() { + viewController.traitOverrides.horizontalSizeClass = .compact + controller.configure(Tab.allCases, in: viewController) { _ in } + + #expect(viewController.navigationItem.leftBarButtonItems == [doneButton]) + #expect(!viewController.navigationItem.leftItemsSupplementBackButton) + } + + @Test func changingToCompactWidthRemovesOnlyTheSegmentedControl() { + viewController.traitOverrides.horizontalSizeClass = .regular + controller.configure(Tab.allCases, in: viewController) { _ in } + + viewController.traitOverrides.horizontalSizeClass = .compact + viewController.updateTraitsIfNeeded() + + #expect(viewController.navigationItem.leftBarButtonItems == [doneButton]) + } + + @Test func changingBackToRegularWidthAddsTheSegmentedControlOnce() { + viewController.traitOverrides.horizontalSizeClass = .regular + controller.configure(Tab.allCases, in: viewController) { _ in } + + viewController.traitOverrides.horizontalSizeClass = .compact + viewController.updateTraitsIfNeeded() + viewController.traitOverrides.horizontalSizeClass = .regular + viewController.updateTraitsIfNeeded() + + let items = viewController.navigationItem.leftBarButtonItems ?? [] + #expect(items.count == 2) + #expect(items.first === doneButton) + #expect(items.last?.customView === controller.segmentedControl) + } +} + +private enum Tab: String, CaseIterable, AdaptiveTabBarItem { + case traffic + case insights + + var id: String { rawValue } + var localizedTitle: String { rawValue } +} diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 85dd779d7831..156c5f6e41a5 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -5,6 +5,7 @@ * [*] Fix the preview of a self-hosted post opened from Spotlight closing a second after it opens [#26223] * [*] iPad: Fix notices being cut off or covering the tab bar after resizing the app window [#26204] * [*] Reader: Fix the spacing around headings in posts [#26170] +* [*] Stats: Fix the back button missing on iPhone Duo and on Plus and Pro Max iPhones in landscape [#26207] 27.4