Vstack到屏幕中心

发布于 2025-02-03 04:37:34 字数 4738 浏览 3 评论 0原文

我必须显示以下内容,使用三个VSTACK显示它。但是,登录vstack是到屏幕底部。如何将其放置到屏幕的中心,然后将Google图像放在大多数屏幕上

    struct Login: View {
       var body: some View {
      ZStack{
                CustomNavigationBar() // To diplay image
              VStack{
                LoginView(lgnvwmmodel: lgnvwmmodel) //To display Login View
                HelpView()
                }
            }
    }
    
    }



struct CustomNavigationBar: View{
    var body: some View{
        NavigationView{
        Image(uiImage: UIImage(named: "google.png")!)
        } .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitleDisplayMode(.inline)
            .padding(0)
    }
}

struct LoginView: View {
    
    @State private var emailId: String = ""
    @State private var password: String = ""
    @State var ntwrkShowAlert = false
    @State private var success = false
    @State var isModal:Bool = false
    @EnvironmentObject var DataGetting : DataStorage
    @ObservedObject var monitor = NetworkMonitor()
    @State private var isEmailValid : Bool   = true
    @State private var showingAlert = false
    @State var chngpasscode = false
    @State var btnClicked:CancelClicked = .none
    @State var showPopup = false
    @ObservedObject var lgnvwmmodel : LoginViewModel
   var body: some View {
       Text("Login")
            .font(.custom("OpenSans-Bold", size: 24))
            .padding(.top, 100)
        
        
        TextField("Email ID", text: $emailId)
            .onReceive(Just(emailId), perform: { _ in
                if emailId.count >= 50{
                    emailId = String(emailId.prefix(50))
                }
            })
            .modifier(customViewModifier(roundedCornes: 6,  textColor: .black))
            .frame(height: 100)
            .padding([.leading, .trailing], 100)

        
        SecureField("Password", text: $password)
            .onReceive(Just(password), perform: { _ in
                if password.count >= 50{
                    password = String(password.prefix(50))
                }
            })
            .modifier(customViewModifier(roundedCornes: 6,  textColor: .black))
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
       
        
        
        Button(action: {
            
        }, label: {
            Text("Forgot Password ?")
                .underline()
                .frame( height: 40, alignment: .trailing)
                .padding(.leading, 320)
            
        })
        
        Button(action: {
     
            if(DataGetting.strEmail == ""){
    UserDefaults.standard.setValue(self.emailId, forKey:"Email")
            }
            if((DataGetting.strPassword) == 0){
                UserDefaults.standard.setValue(self.password, forKey: "Password")
            }
            
            
            self.isModal = true



            if(monitor.status == .connected){
                self.ntwrkShowAlert = false
                lgnvwmmodel.dataStorage = DataGetting
            lgnvwmmodel.getUserIdDetails(emailId: emailId, password: password){ status in
                do {
                    if(status){
                       
                        success = true
                            
                    }
                }
            }
            
            }
            else{
                self.ntwrkShowAlert = true
                    
            }
            
            
        }, label: {
            Text("Sign In")
                .frame(width: 200, height: 30)
                .padding()
                .background(Color.red)
                .foregroundColor(Color.white)
                .font(.custom("OpenSans-Bold", size: 28))
            
        }).alert(isPresented: $ntwrkShowAlert){
      
      return Alert(title: Text("Please check your internet connection"), message: Text(""), dismissButton: .default(Text("OK")))
          }

        
       
       
    }


}

struct HelpView: View{
    @State private var isAlert = false
    @State private var errorMessage: String = ""
    var body: some View{
    Button(action: {
        
    }, label: {
        Text("Help")
            .frame(minWidth: 0, maxWidth: .infinity, maxHeight: 80)
            .background(Color.red)
            .foregroundColor(Color.white)
            .font(.custom("OpenSans-Bold", size: 28))
    }).alert(isPresented: $isAlert){
        Alert(title: Text(self.errorMessage), message: Text(""), dismissButton: .default(Text("OK")))
    }
}
}

“ https://i.sstatic.net/xi8bv.png” alt =“在此处输入图像说明”>

I have to display like the following , used three VStacks to display it. However the login VStack is to the bottom of the screen. How to place it to the center of the screen and Google image to the top most of the screen

    struct Login: View {
       var body: some View {
      ZStack{
                CustomNavigationBar() // To diplay image
              VStack{
                LoginView(lgnvwmmodel: lgnvwmmodel) //To display Login View
                HelpView()
                }
            }
    }
    
    }



struct CustomNavigationBar: View{
    var body: some View{
        NavigationView{
        Image(uiImage: UIImage(named: "google.png")!)
        } .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitleDisplayMode(.inline)
            .padding(0)
    }
}

struct LoginView: View {
    
    @State private var emailId: String = ""
    @State private var password: String = ""
    @State var ntwrkShowAlert = false
    @State private var success = false
    @State var isModal:Bool = false
    @EnvironmentObject var DataGetting : DataStorage
    @ObservedObject var monitor = NetworkMonitor()
    @State private var isEmailValid : Bool   = true
    @State private var showingAlert = false
    @State var chngpasscode = false
    @State var btnClicked:CancelClicked = .none
    @State var showPopup = false
    @ObservedObject var lgnvwmmodel : LoginViewModel
   var body: some View {
       Text("Login")
            .font(.custom("OpenSans-Bold", size: 24))
            .padding(.top, 100)
        
        
        TextField("Email ID", text: $emailId)
            .onReceive(Just(emailId), perform: { _ in
                if emailId.count >= 50{
                    emailId = String(emailId.prefix(50))
                }
            })
            .modifier(customViewModifier(roundedCornes: 6,  textColor: .black))
            .frame(height: 100)
            .padding([.leading, .trailing], 100)

        
        SecureField("Password", text: $password)
            .onReceive(Just(password), perform: { _ in
                if password.count >= 50{
                    password = String(password.prefix(50))
                }
            })
            .modifier(customViewModifier(roundedCornes: 6,  textColor: .black))
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
       
        
        
        Button(action: {
            
        }, label: {
            Text("Forgot Password ?")
                .underline()
                .frame( height: 40, alignment: .trailing)
                .padding(.leading, 320)
            
        })
        
        Button(action: {
     
            if(DataGetting.strEmail == ""){
    UserDefaults.standard.setValue(self.emailId, forKey:"Email")
            }
            if((DataGetting.strPassword) == 0){
                UserDefaults.standard.setValue(self.password, forKey: "Password")
            }
            
            
            self.isModal = true



            if(monitor.status == .connected){
                self.ntwrkShowAlert = false
                lgnvwmmodel.dataStorage = DataGetting
            lgnvwmmodel.getUserIdDetails(emailId: emailId, password: password){ status in
                do {
                    if(status){
                       
                        success = true
                            
                    }
                }
            }
            
            }
            else{
                self.ntwrkShowAlert = true
                    
            }
            
            
        }, label: {
            Text("Sign In")
                .frame(width: 200, height: 30)
                .padding()
                .background(Color.red)
                .foregroundColor(Color.white)
                .font(.custom("OpenSans-Bold", size: 28))
            
        }).alert(isPresented: $ntwrkShowAlert){
      
      return Alert(title: Text("Please check your internet connection"), message: Text(""), dismissButton: .default(Text("OK")))
          }

        
       
       
    }


}

struct HelpView: View{
    @State private var isAlert = false
    @State private var errorMessage: String = ""
    var body: some View{
    Button(action: {
        
    }, label: {
        Text("Help")
            .frame(minWidth: 0, maxWidth: .infinity, maxHeight: 80)
            .background(Color.red)
            .foregroundColor(Color.white)
            .font(.custom("OpenSans-Bold", size: 28))
    }).alert(isPresented: $isAlert){
        Alert(title: Text(self.errorMessage), message: Text(""), dismissButton: .default(Text("OK")))
    }
}
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2025-02-10 04:37:34

这是固定的简化版本(没有模型或按钮操作)。我不确定为什么您将Google徽标包裹在NavigationView中? - 因此,我跳过了它,因为它会引起布局问题。

struct ContentView: View {
    var body: some View {
        VStack{
            CustomNavigationBar()
            Spacer()
            LoginView()
            Spacer()
            HelpView()
        }
    }
}


struct CustomNavigationBar: View{
    var body: some View{
            Image("google")
    }
}


struct LoginView: View {
    
    @State private var emailId: String = ""
    @State private var password: String = ""
    
    var body: some View {
        
        Text("Login")
            .font(.custom("OpenSans-Bold", size: 24))
        
        TextField("Email ID", text: $emailId)
            .padding().border(.black, width: 1)
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
        
        SecureField("Password", text: $password)
            .padding().border(.black, width: 1)
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
        Button(action: {
        }, label: {
            Text("Forgot Password ?")
                .underline()
                .frame( height: 40, alignment: .trailing)
                .padding(.leading, 320)
            
        })
        
        Button(action: {
        }, label: {
            Text("Sign In")
                .frame(width: 200, height: 30)
                .padding()
                .background(Color.red)
                .foregroundColor(Color.white)
                .font(.custom("OpenSans-Bold", size: 28))
            
        })
    }
}

struct HelpView: View{
    var body: some View{
        Button(action: {
        }, label: {
            Text("Help")
                .frame(maxWidth: .infinity, maxHeight: 80)
                .background(.red)
                .foregroundColor(.white)
                .font(.custom("OpenSans-Bold", size: 28))
        })
    }
}

Here is a fixed simplified version (without models or button actions). I'm not sure why you wrapped the Google logo in a NavigationView? – so I skipped it as it causes layout issues.

enter image description here

struct ContentView: View {
    var body: some View {
        VStack{
            CustomNavigationBar()
            Spacer()
            LoginView()
            Spacer()
            HelpView()
        }
    }
}


struct CustomNavigationBar: View{
    var body: some View{
            Image("google")
    }
}


struct LoginView: View {
    
    @State private var emailId: String = ""
    @State private var password: String = ""
    
    var body: some View {
        
        Text("Login")
            .font(.custom("OpenSans-Bold", size: 24))
        
        TextField("Email ID", text: $emailId)
            .padding().border(.black, width: 1)
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
        
        SecureField("Password", text: $password)
            .padding().border(.black, width: 1)
            .frame(height: 100)
            .padding([.leading, .trailing], 100)
        
        Button(action: {
        }, label: {
            Text("Forgot Password ?")
                .underline()
                .frame( height: 40, alignment: .trailing)
                .padding(.leading, 320)
            
        })
        
        Button(action: {
        }, label: {
            Text("Sign In")
                .frame(width: 200, height: 30)
                .padding()
                .background(Color.red)
                .foregroundColor(Color.white)
                .font(.custom("OpenSans-Bold", size: 28))
            
        })
    }
}

struct HelpView: View{
    var body: some View{
        Button(action: {
        }, label: {
            Text("Help")
                .frame(maxWidth: .infinity, maxHeight: 80)
                .background(.red)
                .foregroundColor(.white)
                .font(.custom("OpenSans-Bold", size: 28))
        })
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文