0

Hopefully, this is an easy question. I am attempting to pass to the FilterViewa struct from the FilterView_Previews struct @Binding value that is a Bool like this:

import SwiftUI

struct FilterView: View {
     @Binding var isNavigationBarHidden: Bool
    
    var body: some View {
        ZStack {
            Text("Filters go here")
        }
        .navigationBarTitle("")
        .onAppear {
            self.isNavigationBarHidden = false
        }
    }
}

#if DEBUG
struct FilterView_Previews: PreviewProvider {
    var isHidden: Bool = true
    
    static var previews: some View {
        FilterView(isNavigationBarHidden: isHidden)
    }
}
#endif

However, the value isHidden is getting flagged with a 'Cannot convert value of type 'Bool' to expected argument type 'Binding'. In this scenario, how do you create an appropriate @Binding value within the FilterView_Previews struct that satisfies the compiler?

Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52
  • 1
    Does this answer your question? [SwiftUI @Binding Initialize](https://stackoverflow.com/questions/56685964/swiftui-binding-initialize) – pawello2222 Jun 26 '20 at 17:27

1 Answers1

0

You can try using a constant Binding when in previews:

struct FilterView_Previews: PreviewProvider {
    static var previews: some View {
        FilterView(isNavigationBarHidden: .constant(true))
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • That does work and thank you for that. How would you create a variable that would be a Binding value rather than just passing in a constant? – Perry Hoekstra Jun 26 '20 at 17:26
  • @PerryHoekstra Actually I've just realised this question has already been asked before. Please refer to the post I suggested for more explanation. – pawello2222 Jun 26 '20 at 17:28