在视图层次结构顶部显示自定义弹出视图 ~ SwiftUI
Show custom popup view on top of view Hierarchy ~ SwiftUI
由于自定义弹出视图显示完美但是导航视图后退按钮在显示弹出视图时也启用。那么,是否可以像我们通常在 swift 中那样在视图层次结构顶部显示自定义弹出视图
guard let window : UIWindow = UIApplication.shared.windows.filter({[=10=].isKeyWindow}).first else {return}
var presentVC = window.rootViewController
while let next = presentVC?.presentedViewController {
presentVC = next
}
源代码:
struct LogInView: View {
@State private var email: String = ""
@State private var isShowPopup: Bool = false
@Binding var showSelfView: Bool
var body: some View {
ZStack {
VStack(alignment: .leading, spacing: 30) {
// Top
VStack(alignment: .leading, spacing: 10) {
Text("Login")
.font(.title).bold()
.foregroundColor(Design.Theme.PrimaryTextColor)
Text("Please enter your password to log in to your VaultsPay account")
.font(.headline)
.fontWeight(.regular)
.foregroundColor(Design.Theme.SecondaryTextColor)
}
// TextFields
VStack(spacing: 15) {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textFieldStyle(.plain)
.padding(.horizontal, 12)
.frame(height: 55)
.background(
Design.Theme.TextFieldBackgroundColor.cornerRadius(15)
)
TextField("Password", text: $email)
.keyboardType(.emailAddress)
.textFieldStyle(.plain)
.frame(height: 55)
.padding(.horizontal, 12)
.background(
Design.Theme.TextFieldBackgroundColor.cornerRadius(15)
)
}
Button(action: {
withAnimation {
self.isShowPopup.toggle()
}
}) {
Text("Login")
.font(.title3).bold()
.foregroundColor(Design.Theme.WhiteTextColor)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Design.Theme.SecondaryBackgroundColor)
.cornerRadius(15)
.opacity(0.4)
}
HStack(alignment: .center) {
Button(action: {
self.showSelfView = false
}) {
Text("Forgot Password?")
.font(.headline).bold()
.foregroundColor(Design.Theme.TertiaryTextColor)
.frame(width: 200, height: 25)
}
.frame(maxWidth: .infinity)
}
Spacer()
} //: Main VSTACK
.padding(.horizontal)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
Image(Design.Icon.AppLogo)
.scaledToFit()
}
}
if self.isShowPopup {
GeometryReader { _ in
ConfirmationDialog()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.horizontal, 35)
}
.background(
Color.black.opacity(0.65)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
self.isShowPopup.toggle()
}
}
)
}
}
}
处理自定义弹出视图的源代码:
if self.isShowPopup {
GeometryReader { _ in
ConfirmationDialog()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.horizontal, 35)
}
.background(
Color.black.opacity(0.65)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
self.isShowPopup.toggle()
}
}
)
}
问题截图:
提前致谢。期待您的帮助。
在VStack
的底部添加修饰符.navigationBarBackButtonHidden()
。
...
} //: Main VSTACK
.padding(.horizontal)
.navigationBarBackButtonHidden(isShowPopup) // <- Here
.navigationBarTitleDisplayMode(.inline)
.toolbar {
...
由于自定义弹出视图显示完美但是导航视图后退按钮在显示弹出视图时也启用。那么,是否可以像我们通常在 swift 中那样在视图层次结构顶部显示自定义弹出视图
guard let window : UIWindow = UIApplication.shared.windows.filter({[=10=].isKeyWindow}).first else {return}
var presentVC = window.rootViewController
while let next = presentVC?.presentedViewController {
presentVC = next
}
源代码:
struct LogInView: View {
@State private var email: String = ""
@State private var isShowPopup: Bool = false
@Binding var showSelfView: Bool
var body: some View {
ZStack {
VStack(alignment: .leading, spacing: 30) {
// Top
VStack(alignment: .leading, spacing: 10) {
Text("Login")
.font(.title).bold()
.foregroundColor(Design.Theme.PrimaryTextColor)
Text("Please enter your password to log in to your VaultsPay account")
.font(.headline)
.fontWeight(.regular)
.foregroundColor(Design.Theme.SecondaryTextColor)
}
// TextFields
VStack(spacing: 15) {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textFieldStyle(.plain)
.padding(.horizontal, 12)
.frame(height: 55)
.background(
Design.Theme.TextFieldBackgroundColor.cornerRadius(15)
)
TextField("Password", text: $email)
.keyboardType(.emailAddress)
.textFieldStyle(.plain)
.frame(height: 55)
.padding(.horizontal, 12)
.background(
Design.Theme.TextFieldBackgroundColor.cornerRadius(15)
)
}
Button(action: {
withAnimation {
self.isShowPopup.toggle()
}
}) {
Text("Login")
.font(.title3).bold()
.foregroundColor(Design.Theme.WhiteTextColor)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Design.Theme.SecondaryBackgroundColor)
.cornerRadius(15)
.opacity(0.4)
}
HStack(alignment: .center) {
Button(action: {
self.showSelfView = false
}) {
Text("Forgot Password?")
.font(.headline).bold()
.foregroundColor(Design.Theme.TertiaryTextColor)
.frame(width: 200, height: 25)
}
.frame(maxWidth: .infinity)
}
Spacer()
} //: Main VSTACK
.padding(.horizontal)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
Image(Design.Icon.AppLogo)
.scaledToFit()
}
}
if self.isShowPopup {
GeometryReader { _ in
ConfirmationDialog()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.horizontal, 35)
}
.background(
Color.black.opacity(0.65)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
self.isShowPopup.toggle()
}
}
)
}
}
}
处理自定义弹出视图的源代码:
if self.isShowPopup {
GeometryReader { _ in
ConfirmationDialog()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.horizontal, 35)
}
.background(
Color.black.opacity(0.65)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
self.isShowPopup.toggle()
}
}
)
}
问题截图:
提前致谢。期待您的帮助。
在VStack
的底部添加修饰符.navigationBarBackButtonHidden()
。
...
} //: Main VSTACK
.padding(.horizontal)
.navigationBarBackButtonHidden(isShowPopup) // <- Here
.navigationBarTitleDisplayMode(.inline)
.toolbar {
...