5

I'm playing around with creating subviews and got this error:

SignInButtons.swift:54:34: error: cannot convert value of type 'String' to expected argument type 'Binding<String>'
                      signInUrl: "http://www.apple.com",
                                 ^~~~~~~~~~~~~~~~~~~~~~struct 

I also got this:

'ContentView' initializer is inaccessible due to 'private' protection level

====================

ContentView: View {

import SwiftUI

struct ContentView: View {
    //our color palette
    let colorWheel : [String: Color] = [
        "darkOrange"    : Color.init(hex: "F1615D"),
        "mediumOrange"  : Color.init(hex: "FF8761"),
        "darkYellow"    : Color.init(hex: "FFC575"),
        "lightYellow"   : Color.init(hex: "F1FAC6"),
        "brightAqua"    : Color.init(hex: "79E6E3"),
        "lightAqua"     : Color.init(hex: "a8e6cf"),
        "limeGreen"     : Color.init(hex: "dcedc1"),
        "brightPeach"   : Color.init(hex: "ff8b94"),
        "mediumPeach"   : Color.init(hex: "ffaaa5"),
        "lightPeach"    : Color.init(hex: "ffd3b6"),
    ]

    @State private var backgroundColor:Color
    @State private var signInUrl = "http://www.apple.com"
    @State private var showModal = false
    private var detailSize = CGSize(width: 0, height: UIScreen.main.nativeBounds.height)

    var body: some View {
        ZStack{
            VStack {
                AnimatedContentView()
                Spacer()

                SignInButtons(backgroundColor: self.backgroundColor,
                              signInUrl: self.$signInUrl,
                              showModal: self.$showModal)

                Spacer().frame(height: CGFloat(100))
            }
            .onAppear() {
                self.backgroundColor = self.colorWheel["darkOrange"]!
            }
            .foregroundColor(Color.black.opacity(0.7))
            .edgesIgnoringSafeArea(.all)

            VStack {
                AskForEmailView(showModal: self.$showModal)
                .offset( self.showModal ? CGSize.zero : detailSize)

            }

        } //end zStack
    }
}



struct ContentView_Previews: PreviewProvider {
    enum MyDeviceNames: String, CaseIterable {
        case iPhoneXrMax = "iPhone 11 Pro Max"
//        case iphoneX = "iPhone X"
//        case iPhoneXs = "iPhone Xs"
//        case iPhoneXsMax = "iPhone Xs Max"
//        case iPad = "iPad Pro (11-inch)"

        static var all: [String] {
            return MyDeviceNames.allCases.map { $0.rawValue }
        }
    }
    static var previews: some View {
        //ContentView()
        Group {
            ForEach(MyDeviceNames.all, id: \.self) { deviceName in
                ContentView(detailSize: CGSize.zero)
                      .previewDevice(PreviewDevice(rawValue: deviceName))
                      .previewDisplayName(deviceName)
            }
//            ForEach(MyDeviceNames.all, id: \.self) { deviceName in
//                ContentView()
//                    .colorScheme(.dark)
//                    .background(Color.black)
//                    .edgesIgnoringSafeArea(.all)
//                    .previewDevice(PreviewDevice(rawValue: deviceName))
//                    .previewDisplayName(deviceName)
//            }
        }

    }
}

import SwiftUI

struct SignInButtons: View {
    var backgroundColor:Color
    @Binding var signInUrl : String
    @Binding var showModal : Bool

    var body: some View {
        VStack(spacing: 0) {
            //using text w tap gesture - to do: make into its own component
            Text("Sign in with Facebook")
                .fontWeight(.light)
                .font(.title)
                .padding()
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 70, alignment: .top)
                .background(Color.blue.opacity(0.8))
                .foregroundColor(Color.white)
                .onTapGesture {
                    self.signInUrl = "http://www.facebook.com"
                    self.showModal.toggle()
                }

            //using a button
            Button(action: {
                print("Button Pushed")
                self.signInUrl = "http://www.google.com"
                self.showModal.toggle()
            }) {
                Text("Sign in with Google")
                    .fontWeight(.light)
                    .font(.title)
                    .padding()
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 70, alignment: .top)
                    .background(backgroundColor)
                    .foregroundColor(Color.white)
            }
        }
        .navigationBarHidden(true)
        .edgesIgnoringSafeArea([.top, .bottom])
    }
}

struct SignInButtons_Previews: PreviewProvider {
    static var previews: some View {
        SignInButtons(backgroundColor: Color.blue,
                      signInUrl: "http://www.apple.com",
                      showModal: false)
    }
}

1 Answer 1

12

Here is possible approach - to use constant binding for Preview

struct SignInButtons_Previews: PreviewProvider {
    static var previews: some View {
        SignInButtons(backgroundColor: Color.blue,
                      signInUrl: .constant("http://www.apple.com"),
                      showModal: .constant(false))
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

why you always find a work around for this, what if i need to use the variable since it can be generic value, this is the second answer i see and still can't set Text() using a string from an observedObject :((
@MostafaSultan, for you - stackoverflow.com/a/60632545/12299030

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.