ios 在视图层次结构的顶部显示自定义弹出视图~SwiftUI

pepwfjgg  于 2022-12-05  发布在  iOS
关注(0)|答案(2)|浏览(149)

As custom Popup view is showing perfectly But navigation view back button is also enable while showing popup view. So, Is it possible to show custom popup view on top of view hierarchy like we normally do in swift like this

guard let window : UIWindow = UIApplication.shared.windows.filter({$0.isKeyWindow}).first else {return}
        var presentVC = window.rootViewController
        while let next = presentVC?.presentedViewController {
            presentVC = next
        }

Source Code:

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()
                        }
                    }
            )
        }
        
    }
    
}

Source code to handle custom Popup view:

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()
                        }
                    }
            )
        }

Problem Screenshot:

Thanks in advance. Looking forward to your help.

z2acfund

z2acfund1#

VStack的底部,加入修饰词.navigationBarBackButtonHidden()

...

                    } //: Main VSTACK
                    .padding(.horizontal)
                    .navigationBarBackButtonHidden(isShowPopup)  // <- Here
                    .navigationBarTitleDisplayMode(.inline)
                    .toolbar {

        ...
7lrncoxx

7lrncoxx2#

我知道这是一个老问题,但搜索了这么多的实现弹出在swift用户界面和每件事,我发现有问题,无论是导航栏,返回按钮的屏幕大小,除非我发现以下链接. https://johncodeos.com/how-to-create-a-popup-window-with-swiftui/
简单地创建一个弹出视图,并根据绑定变量显示和隐藏它。在ZStack中调用这个弹出视图,如下所示-很高兴找到这个!!!希望它也能帮助其他人!!!

struct ComtentView: View {
        @State var selection: Bool = false
    var body: some View {
        ZStack{
                NavigationView {
                ...... your custom code
                      Button {
                                
                                print("button is tapped")
                                self.selection = true
                }
                Popupview (show: $selection)
         }
    }
    }
    
struct PopupView: View {
    @Binding var show:Bool
.... your custom code
}

相关问题