Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import androidx.compose.ui.unit.Dp
*
* - Android and iOS < 26: Material [MaterialNavigationBar].
* - iOS 26+: the tab bar of a native UITabBarController, so it gets the Liquid Glass look and follows the system
* placement: at the bottom, or on the side on iPhone Duo.
* placement: at the bottom, on the side on iPhone Duo, or at the top on iPad.
*
* Place this on top of the content, filling the screen: the Material bar is drawn at the bottom.
*
* @param onHeightChanged reports the height of the bar, including the bottom system insets. 0 when the bar is on the side.
* @param onHeightChanged reports the height of the bar, including the bottom system insets. 0 when the bar is on the side or at the top.
* Provide it as [LocalAppNavigationBarHeight] to the content behind the bar.
*/
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ internal fun NativeTabBar(
}
}

/** Space the tab bar takes at the bottom of the screen, including the bottom safe area. 0 when it is on the side. */
/**
* Space the tab bar takes at the bottom of the screen, including the bottom safe area.
* 0 when it is on the side or at the top.
*/
@OptIn(ExperimentalForeignApi::class)
private fun NativeTabBarController.bottomTabBarHeight(): Double {
if (tabBar.hidden) return 0.0
val isVertical = tabBar.frame.useContents { size.height > size.width }
if (isVertical) return 0.0
if (isTabBarVertical || isTabBarAtTop) return 0.0
val viewHeight = view.bounds.useContents { size.height }
val tabBarTop = tabBar.frame.useContents { origin.y }
return (viewHeight - tabBarTop).coerceIn(0.0, viewHeight)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nl.q42.template.core.ui.compose.composables.navigation

import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.useContents
import nl.q42.template.core.ui.util.isLiquidGlassAvailable
import platform.UIKit.UIEdgeInsetsMake
import platform.UIKit.UIImage
import platform.UIKit.UITab
import platform.UIKit.UITabBarController
Expand All @@ -10,14 +12,15 @@ import platform.UIKit.UIViewAutoresizingFlexibleHeight
import platform.UIKit.UIViewAutoresizingFlexibleWidth
import platform.UIKit.UIViewController
import platform.UIKit.addChildViewController
import platform.UIKit.additionalSafeAreaInsets
import platform.UIKit.beginAppearanceTransition
import platform.UIKit.endAppearanceTransition
import platform.UIKit.didMoveToParentViewController

/**
* Root view controller on iOS 26+: a real UITabBarController, with the Compose view controller on top of its
* (empty) tabs. Only the bar of a UITabBarController follows the system placement: at the bottom, or on the side
* of the screen on iPhone Duo. A standalone UITabBar always stays at the bottom.
* (empty) tabs. Only the bar of a UITabBarController follows the system placement: at the bottom, on the side
* of the screen on iPhone Duo, or at the top on iPad. A standalone UITabBar always stays at the bottom.
*
* Navigation stays in Compose, [NativeTabBar] sets the tabs and the selected tab.
* The Compose view controller never moves into a tab: Compose disposes everything when its view leaves the window.
Expand Down Expand Up @@ -49,6 +52,7 @@ internal class NativeTabBarController(
override fun viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
keepContentAboveTabs()
keepContentBelowTopTabBar()
onLayout()
}

Expand All @@ -75,12 +79,38 @@ internal class NativeTabBarController(
if (selectedTab != tab) selectedTab = tab
}

/** On iPhone Duo the tab bar can be a rail on the side of the screen. */
internal val isTabBarVertical: Boolean
get() = tabBar.frame.useContents { size.height > size.width }

/** On iPad the tab bar can be at the top of the screen instead of at the bottom. */
internal val isTabBarAtTop: Boolean
get() {
if (isTabBarVertical) return false
val tabBarCenterY = tabBar.frame.useContents { origin.y + size.height / 2 }
return tabBarCenterY < view.bounds.useContents { size.height / 2 }
}

/** Adds a top tab bar to the safe area of the Compose view controller, so screens lay out below it. */
private fun keepContentBelowTopTabBar() {
val topInset = if (!tabBar.hidden && isTabBarAtTop) {
val tabBarBottom = tabBar.frame.useContents { origin.y + size.height }
(tabBarBottom - view.safeAreaInsets.useContents { top }).coerceAtLeast(0.0)
} else {
0.0
}
if (content.additionalSafeAreaInsets.useContents { top } != topInset) {
content.additionalSafeAreaInsets = UIEdgeInsetsMake(topInset, 0.0, 0.0, 0.0)
}
}

/**
* The tabs are empty, Compose draws the content: keeps the Compose view controller a visible child, right above
* the tab views. The tab bar stays on top of Compose.
*/
internal fun keepContentAboveTabs() {
// setTabs removes every other child view controller, and tells it that it disappeared
// After setTabs in updateItems: it removes every child view controller that is not a tab, Compose included,
// and tells it that it disappeared. So re-attach it and tell it that it appeared again.
if (content.parentViewController != this) {
addChildViewController(content)
content.didMoveToParentViewController(this)
Expand Down
Loading