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下工具栏不显示的效果:
内容的提问来源于stack exchange,提问作者AkinoAvx13
相关产品推荐
相关产品推荐


