'Full Screen Modal not being triggered when displaying in another view
I currently have a view displayed in a controller. The view should return two buttons that when toggled, displays a full screen sheet directing to another view.
The view works as expected in the canvas preview, but when displayed in the controller the buttons don't toggle the sheets.
I'm wondering if the error is due to variables in the view tab aren't carrying over to the controller, which results in the view not being triggered effectively.
View:
struct AddToolbar: View {
@Binding var showAddOptions: Bool
@State var showMealView = false
var body: some View {
if showAddOptions{
Color.black.opacity(showAddOptions ? 0.5 : 1).edgesIgnoringSafeArea(.all)
HStack{
VStack{
Button(action: {
showMealView.toggle()
}){
VStack{
Image(systemName: "square.and.pencil")
.font(.title)
.foregroundColor(.black)
.background(Circle()
.fill(.gray)
.frame(width:50, height:50))
.padding(3)
Text("Meal")
.foregroundColor(.black)
}
}.fullScreenCover(isPresented: $showMealView){
JournalEntryMain()
}
}
VStack{
Image(systemName: "square.and.pencil")
.font(.title)
.background(Circle()
.fill(.gray)
.frame(width:50, height:50))
.padding(3)
Text("Recipe")
.foregroundColor(.black)
}
}
.frame(height:10)
}
}
}
struct AddToolbar_Previews: PreviewProvider {
static var previews: some View {
AddToolbar(showAddOptions: Binding.constant(true))
}
}
Controller:
struct UserDashController: View {
// @State private var showMealView = false
@State private var showSettingsView = false
@State private var showAddViews = false
var body: some View {
NavigationView {
VStack{
UserDashboard()
}
//sets setting bar top right
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
VStack{
Button(action: {
showSettingsView.toggle()
}) {
Image(systemName: "line.3.horizontal")
.font(.title3)
.foregroundColor(.black)
}
.sheet(isPresented: $showSettingsView){
JournalEntryMain()
}
}
}
//sets add meal option bottom/center
ToolbarItem(placement: .bottomBar) {
//displaying add meal and recipe icons when clicked
VStack{
if showAddViews {
AddToolbar(showAddOptions: $showAddViews)
.offset(y:-50)
}
Button(action: {
showAddViews.toggle()
}) {
Image(systemName: "plus.circle")
.font(.largeTitle)
.foregroundColor(.blue)
}
}
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
