Keyboard in a sheet's

Viewed 73

I have a basic window with an input field (page 1), on top of it appears a popup window (page 2), inside of which there is also an input fields and buttons, which, when clicked, will bring up a small window with an input field (page 3). If there is no "Done" on the keyboard, the interface functions normally. If you add a "Done" button, it turns out that its color changes from system color blue to gray when moving from page 2 to page 3. Experimenting and wondering why this is so, I found that the toolbar on page 1 is responsible for the color of the button on page 3... If you change the color of the button on the toolbar on page 1 - it will change on the toolbar on page 3, and page 2 will not be affected. Also, adding buttons causes error: "[LayoutConstraints] Unable to simultaneously satisfy constraints." I want to understand why setting the button on the keyboard for Page 1, I also get a button when I type on Page 3? And why is it grayed out and not working? Why if I change the color for the button on Page 1, does it also change for that gray button on Page 3?

A small representative sample:

ContentView

import SwiftUI

struct ContentView: View {
    @State private var bloodClucoseLvl: String = ""
    @State private var isSheetShown: Bool = false
    @FocusState private var focusField: Bool
    var body: some View {
        NavigationView {
            List {
                Section("Add your current blood glucose lvl") {
                    TextField("5,0 mmol/l", text: $bloodClucoseLvl)
                        .focused($focusField)
                }
                Section("Add food or drink") {
                    Button(action:{
                        isSheetShown.toggle()
                    }, label:{
                        HStack{
                            Text("Add")
                            Image(systemName: "folder.badge.plus")
                        }
                    })
                    .sheet(isPresented: $isSheetShown) {
                        addFoodButton()
                    }
                }
            }
            .navigationTitle("Page 1 - General")
            .toolbar{
                ToolbarItem(placement: .keyboard) {
                    HStack {
                        Spacer()
                        Button(action: {
                            focusField = false
                        }) {
                            Text("Done")
                        }
                    }
                }
            }
            .ignoresSafeArea(.keyboard)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

addFoodButton

import SwiftUI

struct addFoodButton: View {
    @State private var selectedFood: String = ""
    @State public var addScreen: Bool = false
    var body: some View {
        ZStack {
            NavigationView {
                List {
                    Section("or choose from category"){
                        NavigationLink(destination: Alcohol(addScreen: $addScreen)){
                            Text("Alcohol")
                        }
                    }
                }
                .listStyle(.insetGrouped)
                .searchable(text: $selectedFood, prompt: "Search by word")
                .navigationTitle("Page 2 - Search in DB")
            }
            if addScreen{
                addScreenView(addScreen: $addScreen)
            }
        }
    }
}

struct Alcohol: View {
    @State private var searchInsideCategory: String = ""
    @Binding var addScreen: Bool
    var body: some View {
        List {
            Button(action: {addScreen.toggle()}){
                Text("Light beer")
            }
        }
        .navigationTitle("Page 2 - Choose beer")
        .searchable(text: $searchInsideCategory, prompt: "Search inside a category")
    }
}

struct addFoodButton_Previews: PreviewProvider {
    static var previews: some View {
        addFoodButton()
    }
}

addScreenView

import SwiftUI

struct addScreenView: View {
    @Binding var addScreen: Bool
    @State private var gram: String = ""
    var body: some View {
        ZStack{
            Color.black.opacity(0.2).ignoresSafeArea()
            VStack(spacing:0){
                Text("Page 3 - Add an item")
                    .bold()
                    .padding()
                Divider()
                VStack(){
                    TextField("gram", text: $gram)
                        .padding(.leading, 16)
                        .padding(.trailing, 16)
                        .keyboardType(.numberPad)
                    Rectangle()
                        .frame(height: 1)
                        .padding(.leading, 16)
                        .padding(.trailing, 16)
                }.padding()
                Divider()
                HStack(){
                    Button(action: {
                        addScreen.toggle()
                    }){
                        Text("Cancel").frame(minWidth:0 , maxWidth: .infinity)
                    }
                    Divider()
                    Button(action: {
                        addScreen.toggle()
                    }){
                        Text("Save").frame(minWidth:0 , maxWidth: .infinity)
                    }
                }.frame(height: 50)
        }
        .background(Color.white.cornerRadius(10))
        .padding([.leading, .trailing], 15)
        }
    }
}

struct addScreenView_Previews: PreviewProvider {
    static var previews: some View {
        addScreenView(addScreen: .constant(true))
    }
}

enter image description here enter image description here

0 Answers
Related