Your First iOS & SwiftUI App: Polishing the App, Episode 4: Dark Mode | raywenderlich.com

Learn how to support dark mode in your iOS apps.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/28797859-your-first-ios-swiftui-app-polishing-the-app/lessons/4

Hi. I cannot click on ‘inspect preview’. The icon is grayed out (along with the two to the left and one to the right). Any ideas why this is happening? How do I ‘un-gray’ it?

I don’t believe I’ve run into this problem yet.

Was it you asking the question here as well?
https://developer.apple.com/forums/thread/702842

Are you seeing this with all SwiftUI projects, or just this one?

Hi. Yes, that’s my post.

I’m not seeing it in any other projects. My ContentView appears to be causing the icons the gray out.

This is what my ContentView looks like:

struct ContentView: View {
    
    @State private var alertIsVisible = false
    @State private var sliderValue = 50.0
    @State private var game = Game()
    
    var body: some View {
        VStack {
            Text("🎯🎯🎯\nPLACE THE BULLSEYE AS CLOSE TO")
                .kerning(2.0)
                .bold()
                .multilineTextAlignment(.center)
                .lineSpacing(4.0)
                .font(.footnote)
            
            Text(String(game.target))
                .fontWeight(.black)
                .kerning(-1.0)
                .font(.largeTitle)
            HStack {
                Text("0").bold()
                Slider(value: $sliderValue, in: 1.0...100.0)
                Text("100").bold()
            }
            Button(action: { alertIsVisible = true }) {
                let roundedSliderValue = Int(sliderValue.rounded())
                
                Text("Hit me")
                    .alert("Hello there!", isPresented: $alertIsVisible) {
                        Button("Awesome!") {}
                    } message: {
                        Text("The slider value is \(roundedSliderValue).\n" +
                             "You scored \(game.points(sliderValue: roundedSliderValue)) points this round.")
                    }
            }
        }
    }
}

The icons become clickable again if I remove:

                    .alert("Hello there!", isPresented: $alertIsVisible) {
                        Button("Awesome!") {}
                    } message: {
                        Text("The slider value is \(roundedSliderValue).\n" +
                             "You scored \(game.points(sliderValue: roundedSliderValue)) points this round.")
                    }

I can’t see any problems with it. It looks like the example in the docs. I’m new to Swift so I might be missing something completely obvious.

I’m using Xcode 13.3 with Swift 5.

In fact, it’s just the message part that causes the problem. Even if the message block is empty.

If I remove it:

  .alert("Hello there!", isPresented: $alertIsVisible) {
      Button("Awesome!") {}
  }

…the buttons are no longer grayed out. It seems an odd side-effect. The message block looks okay to me. I don’t yet understand why it happens.

1 Like

Definitely an Xcode bug. I’m able to work around it by not having message as a trailing closure label. Same behavior for you?

.alert(
  "Hello there!",
  isPresented: $alertIsVisible,
  actions: { Button("Awesome!") {} },
  message: {
    Text("The slider value is \(roundedSliderValue).\n" +
         "You scored \(game.points(sliderValue: roundedSliderValue)) points this round.")
  }
)
.alert(
  "Hello there!",
  isPresented: $alertIsVisible,
  actions: { Button("Awesome!") {} }
) {
  Text("The slider value is \(roundedSliderValue).\n" +
       "You scored \(game.points(sliderValue: roundedSliderValue)) points this round.")
}

Thanks, Jessy. That fixes it. Yes. Same behaviour for me.

1 Like