Monday, January 1, 2024
HomeiOS Developmentios - Unable to alter background shade of the preview from knowledge...

ios – Unable to alter background shade of the preview from knowledge of customized struct in SwiftUI


I am studying Swift and SwiftUI following to this tutorial. The background shade of the preview not modified as anticipated.

The Theme.swift:

import SwiftUI

enum Theme: String {
    // ...others
    case yellow

    var accentColor: Colour {
        change self {
        case /* ...others */ .yellow: return .black // <-- Declare the yellow key for accent shade
        case .indigo, .magenta, .navy, .oxblood, .purple: return .white
        }
    }
    var mainColor: Colour {
        Colour(rawValue)
    }
}

The DailyScrum.swift:

import Basis

struct DailyScrum {
    var title: String
    var attendees: [String]
    var lengthInMinutes: Int
    var theme: Theme
}

extension DailyScrum {
    static let sampleData: [DailyScrum] =
    [
        DailyScrum(title: "Design",
                   attendees: ["Cathy", "Daisy", "Simon", "Jonathan"],
                   lengthInMinutes: 10,
                   theme: .yellow), // <-- assign yellow to the primary merchandise's theme which referred to the `Theme.swift`
        // others
    ]
}

The CardView.swift:

import SwiftUI


struct CardView: View {
    let scrum: DailyScrum
    var physique: some View {
        Textual content("Hi there, World!")
    }
}


struct CardView_Previews: PreviewProvider {
    static var scrum = DailyScrum.sampleData[0] // <-- get first merchandise
    static var previews: some View {
        CardView(scrum: scrum)
            .background(scrum.theme.mainColor) // <-- get the mainColor of the primary merchandise
    }
}

And what I obtained within the preview:
preview of the CardView, the background yellow is meant to seem, however it not.

Anticipated Preview

I’ve tried to alter the scrum.theme.mainColor to .yellow or scrum.theme.accentColor, it really works. I am undecided what’s the issue right here.

My Xcode model is 15, and Swift is 5.9.2.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments