Maksymilian Tomczyk
Maksymilian Tomczyk

Reputation: 1341

ActionSheet renders with incorrect color

My app is using AccentColor defined in Assets.xcassets. I set AccentColor to yellow, however ActionSheets are still using default blue color. Color of button changes after button is touched, but resets to blue after sheet is closed and opened again.

Minimal reproduction

struct ContentView: View {
  @State private var show = false
  var body: some View {
    VStack {
      Button(action: { show = true }) {
        Text("Open")
          .actionSheet(isPresented: $show) {
            let btns: [ActionSheet.Button] = [
              .default(Text(LocalizedStringKey("Button 1")), action: {}),
              .destructive(Text(LocalizedStringKey("Button 2")), action: {}),
              .default(Text(LocalizedStringKey("Button 3")), action: {}),
              .cancel()
            ]
            
            return ActionSheet(title: Text("Action Sheet"), buttons: btns)
          }
      }
    }
  }
}

Simulator device screen recording

Screen recording of iOS Simulator

Upvotes: 2

Views: 148

Answers (1)

Maksymilian Tomczyk
Maksymilian Tomczyk

Reputation: 1341

It looks like a bug (mainly because some actions sheets are rendered with correct color and some are not). I can't find the reason for that, just some sheets are rendered correctly.

Until it's fixed, there is one trick that fixed my problem. It's not perfect, as it's overwrites default tintColor of UIAlertController, but it works. It's as simple as using one line of code in SceneDelegate:

// SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  // ...
  // Any color may be used instead of UIColor(named: "AccentColor")
  UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "AccentColor")
  // ...
}

Upvotes: 1

Related Questions