如何在watchOS中展示.sheet时隐藏关闭按钮(X)?
解决watchOS 10中Sheet默认关闭按钮隐藏问题
在watchOS 10里,系统会自动给.sheet添加左上角的关闭按钮,单纯用EmptyView替换cancellationAction的内容没法隐藏它——因为系统仍会保留该工具栏项的占位。你可以用以下两种可靠方法移除这个按钮:
方法一:直接隐藏指定位置的工具栏项
在Sheet的内容视图中,针对.cancellationAction位置直接隐藏工具栏:
.sheet(isPresented: $isShowingSheet, content: { VStack { Text("Sheet Content") } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.red.gradient) .toolbar(.hidden, for: .cancellationAction) // 关键代码 })
方法二:自定义空工具栏覆盖默认配置
如果需要保留其他工具栏元素,可通过设置空工具栏来覆盖默认的关闭按钮配置:
.sheet(isPresented: $isShowingSheet, content: { VStack { Text("Sheet Content") } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.red.gradient) .toolbar { // 不添加任何ToolbarItem,直接覆盖默认的cancellationAction } })
注意事项
如果Sheet内容嵌套了NavigationStack,需要把工具栏配置放在NavigationStack内部才能生效:
.sheet(isPresented: $isShowingSheet, content: { NavigationStack { VStack { Text("Sheet Content") } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.red.gradient) .toolbar(.hidden, for: .cancellationAction) } })
这样就能彻底移除watchOS 10中Sheet默认的左上角关闭按钮,满足你在workout期间展示UI的需求。
内容的提问来源于stack exchange,提问作者GarySabo
相关产品推荐
相关产品推荐

