我在锻炼期间使用 .sheet 来呈现锻炼 UI。 WatchOS 10 似乎在左上角添加了一个关闭按钮(“X”),通过
.sheet
呈现的视图。我怎样才能隐藏它?建议使用带有空视图的 .cancellationAction
来隐藏它,但这不起作用。还有其他方法可以删除关闭按钮吗?
struct ContentView: View {
@State var isShowingSheet: Bool = false
var body: some View {
NavigationStack {
VStack {
Button {
isShowingSheet = true
} label: {
Text("Show Sheet")
}
}
}
.padding()
.sheet(isPresented: $isShowingSheet, content: {
//NavigationStack { //adding doesn't change
VStack {
Text("Sheet Content")
}
// }
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red.gradient)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
print("button tapped")
}, label: {
EmptyView() //Doesn't hide dismiss button
})
}
}
})
}
}
#Preview {
ContentView()
}
您可以通过引入
NavigationView
来实现,然后您可以使用 .toolbar(.hidden, for: .navigationBar)
隐藏带有“X”按钮的导航栏:
.sheet(isPresented: $isShowingSheet, content: {
NavigationView { // <-- 1
VStack {
Text("Sheet Content")
}
}
.toolbar(.hidden, for: .navigationBar) // <-- 2
// .frame(..)
您可以保留其余代码。只需添加这个额外的
.toolbar(.hidden, for: .navigationBar)
。