TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Why does this Alert code not show colors?

Forums > SwiftUI

import SwiftUI

struct SecondAlert: View {
    @State private var showingAlert = false

    var body: some View {
        Button("Show Alert") {
            showingAlert = true
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Alert!")
                .foregroundColor(.red),
            message: Text("Wear sun screen you dolt!")
                .foregroundColor(.green),
                  dismissButton: .default(Text("OK got it!")))
        }
    }

}

2      

From Documentation

On iOS, tvOS, and watchOS, alerts only support controls with labels that are Text. Passing any other type of view results in the content being omitted.

Only unstyled text is supported for the message.

You can change the Button with role

PS The alert you are using is deprecated you should use

 var body: some View {
    Button("Show Alert") {
        showingAlert = true
    }
    .alert("Alert", isPresented: $showingAlert) {
        Button("Normal") { } // <- Normal text
        Button("Destructive", role: .destructive) { } // <- Red text
        Button("Cancel", role: .cancel) { } // <- Bold text
    } message: {
        Text("Wear sun screen you dolt!")
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.