我已经使用SwiftUI制作了一个自定义警报,但是当我在具有NavigationBar和TabBar的视图中显示它时,我的自定义警报视图没有覆盖这些栏。 Here是带有结果的图像。如您所见,它显示在导航和选项卡栏之间,但我希望我的自定义警报覆盖整个屏幕。有办法解决这个问题吗?
这是我展示自定义提醒的一种方式
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)
}
}
}