Как игнорировать панель навигации и панель вкладок при представлении пользовательского представления в SwiftUI? - PullRequest
1 голос
/ 19 июня 2020

Я создал настраиваемое предупреждение с помощью SwiftUI, но когда я представляю его в представлении, в котором есть NavigationBar и TabBar, мое настраиваемое представление предупреждений не покрывает эти панели. Здесь - изображение с результатом. Как вы можете видеть, это отображается между навигацией и панелью вкладок, но я хочу, чтобы мое настраиваемое оповещение охватило весь экран. Есть ли способ исправить это?

Вот как я представляю свое настраиваемое оповещение

var body: some View {
    TabView {
        NavigationView {
            VStack {
                Text("Demo of CustomAlert")

                NavigationLink(destination: DetailView(), isActive: $goToEmpty) {
                    Text("Open Empty View")
                }

                Button(action: {
                    self.presentAlert.toggle()
                }) {
                    Text("Show Alert")
                        .font(.title)
                }
            }
            .navigationBarTitle("Hello World", displayMode: .inline)
            .alertView(isShowing: $presentAlert) {
                CustomAlert(title: "Title", message: "Message", withTextField: false, placeholder: nil, buttons: [.cancelButton(title: "Okay"), .cancelButton(title: "Cancel")])
            }

        }
    }
}

Код представления настраиваемого оповещения

struct AlertView<Presenting>: View where Presenting: View {
    @Binding var isPresented: Bool
    @ObservedObject var userInput = UserInput()

    let content: CustomAlert
    let presenting: () -> Presenting

    var textAreaColor: Color {
        return userInput.errorText != nil ? Color.red : Color.green
    }

    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(.all).opacity(isPresented ? 0.4 : 0).onTapGesture {
                print("Tap on background")
            }

            self.presenting().disabled(isPresented)

            VStack(alignment: .center, spacing: 0) {
                Text(content.title ?? "")
                    .padding([.horizontal, .bottom, .top], 10)
                    .font(.custom("MavenPro-Bold", size: 18))
                    .fixedSize(horizontal: false, vertical: true)
                    .multilineTextAlignment(.center)
                    .foregroundColor(Color.green)

                Text(content.message ?? "")
                    .padding([.horizontal, .bottom], 10)
                    .font(.custom("MavenPro-Regular", size: 16))
                    .lineSpacing(3)
                    .fixedSize(horizontal: false, vertical: true)
                    .multilineTextAlignment(.center)
                    .foregroundColor(Color.black)

                if content.withTextField {
                    Spacer()
                        .frame(height: 10)
                    TextField(content.placeholder ?? "", text: $userInput.text)
                        .font(.custom("MavenPro-Regular", size: 14))
                        .padding(5)
                        .background(RoundedRectangle(cornerRadius: 5, style: .continuous)
                            .stroke(textAreaColor, lineWidth: 1.5)
                            .background(Color.white))
                        .frame(width: 238, height: 25)
                    Spacer()
                        .frame(height: 2)

                    if userInput.errorText != nil {
                        Text("\(userInput.errorText ?? "Unknown Error")")
                            .foregroundColor(Color.red)
                            .font(.custom("MavenPro-Regular", size: 12))
                            .padding(.top, 6)
                            .frame(width: 234, alignment: .leading)

                    }
                }

                if content.buttons.count > 0 {
                    Divider().background(Color.primary).padding(.top, 10)
                }

                if content.buttons.count == 1 {
                    Button(action: {
                        if !self.content.buttons[0].isDefault {
                            self.userInput.clear()
                            self.isPresented = false
                        }
                        self.content.buttons.first?.action?()
                    }) {
                        Text("\(content.buttons[0].title)")
                            .font(.custom("MavenPro-Bold", size: 18))
                            .foregroundColor(Color.green)
                            .frame(width: 270)
                            .padding(.vertical, 10)
                    }

                } else if content.buttons.count == 2 {
                    TwoButtonsView(isPresented: self.$isPresented, userInput: self.userInput, primaryButton: content.buttons[0], secondaryButton: content.buttons[1])

                } else if content.buttons.count >= 3 {
                    ListButtonsView(isPresented: self.$isPresented, userInput: self.userInput, buttons: content.buttons)
                }
            }
            .background(
                RoundedRectangle(cornerRadius: 14)
                    .foregroundColor(Color.white))
            .frame(width: 270)
            .onAppear(perform: {
                if self.content.buttons.count == 0 {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
                        self.userInput.clear()
                        self.isPresented = false
                    }
                }
            })
            .opacity(isPresented ? 1 : 0)
            .offset(y: -50)

        }
    }
}
...