It's useful to provide code snippet that would need minimum interaction to reproduce the problem. In this case, the ContentView
body
struct ContentView: View {
@State var showActionSheet = false
var body: some View {
NavigationView {
Button(action: { self.showActionSheet = true }) {
Text("Click")
}
.actionSheet(isPresented: $showActionSheet) {
ActionSheet(title: Text("This should be presented once (Button)"))
}
.navigationBarTitle("Title")
.navigationBarItems(trailing: Button(action: { self.showActionSheet = true }) {
Text("Show")
}.actionSheet(isPresented: $showActionSheet) {
ActionSheet(title: Text("This should be presented once (Navigation View)"))
})
}
}
}
I couldn't reproduce the problem in actual iOS (I don't have Catalina), so it could be Preview
bug. In fact, when there is a runtime warning
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
SingleViewTest[] Warning: Attempt to present <UIAlertController> on <UINavigationController> which is already presenting (null)
Which could be the cause, that Preview doesn't know how to respond to this.
Let me explain:
You tried to present the actionSheet
twice, once from view Button
, and again from navigation bar Button
.
When you click either button, showActionSheet
will become true
, and so both actionSheet
s will try to present themselves, but there can be only one actionSheet
presented at any time, hence the warning. You should instead put actionSheet
in a single place so that when either button is clicked, only one actionSheet
will be presented.
actionSheet
set value to false
on dismissal.