You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 16中SwiftUI底部Toolbar不显示问题求解决方案

SwiftUI 底部工具栏iOS 16显示异常的解决办法

你的代码在iOS 15中可以正常显示底部工具栏,但在iOS 16中使用.bottomBar作为ToolbarItemGroup的placement参数时,工具栏会无法显示——这是因为iOS 16对SwiftUI工具栏的布局逻辑做了调整,.bottomBar的行为发生了变化。

以下是两种可行的解决办法:

1. 多版本适配方案

通过条件编译针对不同iOS版本使用对应的placement参数,确保在iOS 15和iOS 16中都能正常显示:

Text("Content")
    .toolbar {
        if #available(iOS 16.0, *) {
            ToolbarItemGroup(placement: .bottomToolbar) {
                Button("Greeting") {
                    print("Hello world!")
                }
            }
        } else {
            ToolbarItemGroup(placement: .bottomBar) {
                Button("Greeting") {
                    print("Hello world!")
                }
            }
        }
    }

2. 仅适配iOS 16+方案

如果你的项目只需要支持iOS 16及以上版本,可以直接将placement参数替换为.bottomToolbar:

Text("Content")
    .toolbar {
        ToolbarItemGroup(placement: .bottomToolbar) {
            Button("Greeting") {
                print("Hello world!")
            }
        }
    }

iOS 16下工具栏不显示的效果:
iOS16底部工具栏不显示

内容的提问来源于stack exchange,提问作者AkinoAvx13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:45:41