0

I am slightly confused with how to properly updating a view button color based on the state. If I am not mistaken, self.cameraOK is true when the user gives the camera access, but the button color does not change. I need to tap on the button again to see the color change. Why is this behaviour happening?

Also any other recommendations for good standards are encouraged as I am fairly new to swift.

Here is the code:

    @State var cameraOK: Bool = false
    @State var photosOK: Bool = false
    
    var body: some View {
        VStack(alignment: .leading) {
            ...
            Button(action: {
                if permissions.giveCameraAccess() {
                    self.cameraOK = true
                }
            })  {
                Text("Enable access to your camera")
                    .font(Font.custom("Inter", size: 16))
                    .foregroundColor(self.cameraOK ? Color(red: 0, green: 1, blue: 0.2) : Color(red: 1, green: 0.68, blue: 0.2) )
                    .fontWeight(.regular)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jmrueda
  • 1,362
  • 19
  • 30
  • 1
    I think *when the user gives the camera access* assumption is incorrect and you just don't get to assignment. Set breakpoint at that line and you see when do you get there. – Asperi Oct 28 '20 at 15:57
  • You were right - the permissions popup is showing, but is jumping to the next instruction before waiting for the popup to get a reply – jmrueda Oct 28 '20 at 16:05
  • For those interested in how to defer asking permission: https://stackoverflow.com/questions/39631256/request-permission-for-camera-and-library-in-ios-10-info-plist – jmrueda Oct 28 '20 at 16:08
  • Show more code: what do you do in `permissions.giveCameraAccess()`? – Asperi Oct 28 '20 at 16:08

0 Answers0