2

编辑:问题的解释

此代码产生一个错误,发布者不知道如何解决它。

这是上一篇文章的新编辑。

我在 >Geometry Reader 上收到错误消息。这篇文章包括所有代码。这篇新帖子包括所要求的注册和登录代码。我希望它是可读的格式。我做了一些更正,希望对您有所帮助。代码如下:

import SwiftUI

struct ContentView: View {

    var body: some View {
       
            Home()
                // for light status bar...
            .preferredColorScheme(.dark)
            
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
    struct Home : View {
        
        @State var index = 0
    
        var body: some View{
            
            GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and  "Required by generic struct 'GeometryReader' where 'Content' = '()'")
            
                VStack{
                
                    Image("logo")
                    .resizable()
                    .frame(width:60, height: 60)
                      
                    ZStack{
                        
                        SignUp(index: self.$index)
                            *// changing view order...*
                            .zIndex(Double(self.index))
                        
                        Login(index: self.$index)
                        
                        
                    }
                    
                    HStack(spacing: 15){
                        
                        Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                        
                        Text("OR")
                        
                        Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                    }
                    .padding(.horizontal, 20)
                    .padding(.top, 50)
                    *// because login button is moved 25 in y axis and 25 padding = 50*
                  
                    
.background(Color("Orange").edgesIgnoringSafeArea(.all))
                    
                    
                    //Curve...
                    
                    HStack(spacing: 25){
                    
                        Button(action: {
                            
                        }) {
                                 Image("Unknown")
                                 .resizable()
                                 .renderingMode(.original)
                                 .frame(width: 50, height: 50)
                                 .clipShape(Circle())
                     }
                                 Button(action: {
                                 }) {
                                     
                                     Image("fb")
                                     .resizable()
                                     .renderingMode(.original)
                                     .frame(width: 50, height: 50)
                                     .clipShape(Circle())
                    }
                                     
                                     Button(action: {
                                     }) {
                                         
                                         Image("instagram")
                                         .resizable()
                                         .renderingMode(.original)
                                         .frame(width: 50, height: 50)
                                         .clipShape(Circle())
                                    
                            }
}
                    .padding(.top, 30)
                }
                .padding(.vertical)
                


struct CShape: Shape {
    func path(in rect: CGRect) -> Path {
        
        return Path {path in
            
            *//right side curve...*
            
            path.move(to: CGPoint(x: rect.width, y: 100))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: 0))
        }
    
}
}
        
        
    struct CShape1: Shape {
        func path(in rect: CGRect) -> Path {
            
            return Path {path in
                
                *//left side curve...*
                
                path.move(to: CGPoint(x: 0, y: 100))
                path.addLine(to: CGPoint(x: 0, y: rect.height))
                path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                path.addLine(to: CGPoint(x: rect.width, y: 0))
            }
        
    }
    }
                
struct Login : View {
            
            @State var email = ""
            @State var pass = ""
            @Binding var index : Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            VStack(spacing:10){
                                
                                Text("Login")
                                    .foregroundColor(self.index == 0 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 0 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                            
                            Spacer(minLength:0)
                        }
                        .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                .foregroundColor(Color("Blue"))
                           
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                    
                    VStack{
                        
                        HStack(spacing:15){
                            
                            Image(systemName: "eye")
                            .foregroundColor(Color("Orange"))
                       
                            SecureField("Password", text: self.$pass)
                        }
                        Divider().background(Color.white.opacity(0.5))
                    }
                    .padding(.horizontal)
                    .padding(.top, 30)
                        
                        HStack{
                            
                            Spacer(minLength: 0)
                            
                            Button(action: {
                                
                            }) {
                                
                                Text("Forget Password?")
                                    .foregroundColor(Color.white.opacity(0.6))
                            }
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                    // bottom padding...
                    .padding(.bottom, 65)
                    .background(Color("LightBlue"))
                    .clipShape(CShape())
                    .contentShape(CShape())
                    .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                    .onTapGesture{
                            
                        self.index = 0
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("LOGIN")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                        .background(Color("LightBlue"))
                        .clipShape(Capsule())
                        // shadow ...
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                    // moving view down...
                        .offset(y: 25)
                        .opacity(self.index == 0 ? 1 : 0)
                }
            }
        }
                
// SignUp Page...
        
struct SignUp : View {
                        
    @State var email = ""
    @State var pass = ""
    @State var Repass = ""
    @Binding var index: Int
                        
        var body : some View {
            
            ZStack(alignment: .bottom) {
                
                VStack{
                    
                    HStack{
                        
                        Spacer(minLength:0)
                        
                        VStack(spacing: 10){
                        
                            Text("SignUp")
                                .foregroundColor(self.index == 1 ? .white : .gray)
                                .font(.title)
                                .fontWeight(.bold)
                        
                            Capsule()
                                .fill(self.index == 1 ? Color.blue : Color.clear)
                                .frame(width:100, height: 5)
                        }
                    }
                    .padding(.top, 30)// for top curve...
                    VStack{
                        
                        HStack(spacing:15){
                            
                            Image(systemName: "envelope")
                            .foregroundColor(Color("Orange"))
                       
                            TextField("Email Adress", text: self.$email)
                        }
                        Divider().background(Color.white.opacity(0.5))
                    }
                    .padding(.horizontal)
                    .padding(.top, 40)
                
                    VStack{
                    
                    HStack(spacing:15){
                        
                        Image(systemName: "eye")
                        .foregroundColor(Color("Orange"))
                   
                        SecureField("Password", text: self.$pass)
                    }
                    Divider().background(Color.white.opacity(0.5))
                }
                    .padding(.horizontal)
                    .padding(.top, 30)
                    
                    // replacing forget password with reenter password...
                    // so same height will be maintained...
                    
                    VStack{
                         
                         HStack(spacing:15){
                             
                             Image(systemName: "eye")
                            .foregroundColor(Color("Orange"))
                        
                             SecureField("Password", text: self.$Repass)
                         }
                         Divider().background(Color.white.opacity(0.5))
                     }
                         .padding(.horizontal)
                         .padding(.top, 30)
                }
                .padding()
                // bottom padding...
                .padding(.bottom, 65)
                .background(Color("Blue"))
                .clipShape(CShape1())
                //clipping the content shape also for tap gesture...
                .contentShape(CShape1())
                // shadow...
                .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                .onTapGesture {
                    
                    self.index = 1
                    
                }
                .cornerRadius(35)
                .padding(.horizontal,20)
                
                *// Button...*
                
                Button(action: {
                    
                }) {
                    
                  Text("SIGNUP")
                    .foregroundColor(.white)
                    .fontWeight(.bold)
                    .padding(.vertical)
                    .padding(.horizontal, 50)
                    .background(Color("Blue"))
                    .clipShape(Capsule())
                    *// shadow ...*
                    .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                }
                *// moving view down...*
                    .offset(y: 25)
            *// hiding view when its in background...*
            *// only button...*
                    .opacity(self.index == 1 ? 1 : 0)

        }
    }
}

}
}

}

        
            
4

1 回答 1

0

所以你的代码中的问题是你在 a 中定义视图GeomtryReader,这是一个很大的不。因此,解决方法是将Loginand移到Singup外部,GeomtryReader甚至更好的做法是为每个视图创建一个新文件并将其代码添加到该文件中。例如,一个文件用于Login.swift,另一个用于Register.swift,也许另一个被调用Shapes,其中包含多个形状并导出它们。

你正在做的事情与此类似

struct ContentView: View {
    var body: some View {
          GeomtryReader { _ in
                Text("test")

                // Here is where the bug would happen
                struct NewView: View {
                      var body: some View {
                            Text("Second View")
                      } 
                }
                //////////////////////////////////////
          }
    }
}

你可以看看如果你复制并粘贴上面的代码它会产生同样的错误。你应该做的是NewView移出GeomtryReader

像这样的东西

struct ContentView: View {
    var body: some View {
          return GeomtryReader { _ in
                Text("test")
          }
          
          // This will fix the error
          struct NewView: View {
                var body: some View {
                      Text("Second View")
                } 
          }
          //////////////////////////////////////
    }
}

注意我将代码移到了哪里。还要注意我已经添加ReturnGeomtryReaderand 那是因为body它是一个计算属性,它期望有一个值,View但是在这种情况下,我们混淆了编译器,View我们希望它是返回值,所以我们必须手动指定它。如果您不想包括在内,return那么您将不得不一起搬到NewView外面body,甚至更好地搬到外面ContentView

无论如何,这是您的代码 100% 工作,您可以复制和粘贴它。

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        
        Home()
            // for light status bar...
            .preferredColorScheme(.dark)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Home : View {
    
    @State var index = 0
    
    var body: some View{
        
        return GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and  "Required by generic struct 'GeometryReader' where 'Content' = '()'")
            
            VStack{
                
                Image("logo")
                    .resizable()
                    .frame(width:60, height: 60)
                
                ZStack{
                    
                    SignUp(index: self.$index)
                        // changing view order...*
                        .zIndex(Double(self.index))
                    
                    Login(index: self.$index)
                    
                    
                }
                
                HStack(spacing: 15){
                    
                    Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                    
                    Text("OR")
                    
                    Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                }
                .padding(.horizontal, 20)
                .padding(.top, 50)
                    // because login button is moved 25 in y axis and 25 padding = 50*
                    
                    
                    .background(Color("Orange").edgesIgnoringSafeArea(.all))
                
                
//                Curve...
                
                HStack(spacing: 25){
                    
                    Button(action: {
                        
                    }) {
                        Image("Unknown")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                    }
                    Button(action: {
                    }) {
                        
                        Image("fb")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                    }
                    
                    Button(action: {
                    }) {
                        
                        Image("instagram")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                        
                    }
                }
                .padding(.top, 30)
            }
            .padding(.vertical)
            
            
            
            
            
            
            
            
            
        }
        
        struct Login : View {
            
            @State var email = ""
            @State var pass = ""
            @Binding var index : Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            VStack(spacing:10){
                                
                                Text("Login")
                                    .foregroundColor(self.index == 0 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 0 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                            
                            Spacer(minLength:0)
                        }
                            .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                    .foregroundColor(Color("Blue"))
                                
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$pass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                        
                        HStack{
                            
                            Spacer(minLength: 0)
                            
                            Button(action: {
                                
                            }) {
                                
                                Text("Forget Password?")
                                    .foregroundColor(Color.white.opacity(0.6))
                            }
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                        // bottom padding...
                        .padding(.bottom, 65)
                        .background(Color("LightBlue"))
                        .clipShape(CShape())
                        .contentShape(CShape())
                        .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                        .onTapGesture{
                            
                            self.index = 0
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("LOGIN")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                            .background(Color("LightBlue"))
                            .clipShape(Capsule())
                            // shadow ...
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                        // moving view down...
                        .offset(y: 25)
                        .opacity(self.index == 0 ? 1 : 0)
                }
            }
        }
        
        //SignUp Page...

        struct SignUp : View {
            
            @State var email = ""
            @State var pass = ""
            @State var Repass = ""
            @Binding var index: Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            Spacer(minLength:0)
                            
                            VStack(spacing: 10){
                                
                                Text("SignUp")
                                    .foregroundColor(self.index == 1 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 1 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                        }
                            .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                    .foregroundColor(Color("Orange"))
                                
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$pass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                        
                        // replacing forget password with reenter password...
                        // so same height will be maintained...
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$Repass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                        // bottom padding...
                        .padding(.bottom, 65)
                        .background(Color("Blue"))
                        .clipShape(CShape1())
                        //clipping the content shape also for tap gesture...
                        .contentShape(CShape1())
                        // shadow...
                        .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                        .onTapGesture {
                            
                            self.index = 1
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...*
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("SIGNUP")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                            .background(Color("Blue"))
                            .clipShape(Capsule())
                            // shadow ...*
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                        // moving view down...*
                        .offset(y: 25)
                        // hiding view when its in background...*
                        // only button...*
                        .opacity(self.index == 1 ? 1 : 0)
                    
                }
            }
        }
        
        struct CShape: Shape {
            func path(in rect: CGRect) -> Path {
                
                return Path {path in
                    
                    //right side curve...*
                    
                    path.move(to: CGPoint(x: rect.width, y: 100))
                    path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                    path.addLine(to: CGPoint(x: 0, y: rect.height))
                    path.addLine(to: CGPoint(x: 0, y: 0))
                }
                
            }
        }

        struct CShape1: Shape {
            func path(in rect: CGRect) -> Path {
                
                return Path {path in
                    
                    //left side curve...*
                    
                    path.move(to: CGPoint(x: 0, y: 100))
                    path.addLine(to: CGPoint(x: 0, y: rect.height))
                    path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                    path.addLine(to: CGPoint(x: rect.width, y: 0))
                }
                
            }
        }
    }
    


}
于 2020-07-21T21:31:20.500 回答