Monday, January 15, 2024
HomeiOS Developmentios - SwiftUI Picker subject with Rectangle() as an alternative of Textual...

ios – SwiftUI Picker subject with Rectangle() as an alternative of Textual content()


It looks as if the difficulty is likely to be with utilizing a customized sort (ColorSquare) as the choice sort for the Picker. The choice sort should conform to the Hashable and Equatable protocols. In your case, ColorSquare conforms to Hashable, however you additionally have to make it conform to Equatable.

This is the modified ColorSquare struct:

struct ColorSquare: View, Hashable, Equatable {
    var colour: Colour
    
    static func == (lhs: ColorSquare, rhs: ColorSquare) -> Bool {
        return lhs.colour == rhs.colour
    }

    func hash(into hasher: inout Hasher) {
        hasher.mix(colour)
    }
    
    var physique: some View {
        Rectangle().fill(colour).aspectRatio(1.0, contentMode: .fill).scaleEffect(0.025)
    }
}

Now, strive updating your TeamColorPicker as follows:

struct TeamColorPicker: View {
    @State personal var selectedColor: Colour = .blue
    var listOfColorsAvailable: [String] = ["Red", "Blue", "Teal", "Green", "Yellow", "Orange"]
    
    var physique: some View {
        Picker("Workforce Colour", choice: $selectedColor) {
            ForEach(listOfColorsAvailable, id: .self) { colorString in
                let colour = Colour(colorString)
                ColorSquare(colour: colour)
                    .tag(colour)
            }
        }
    }
}

P.S: I hope that if this isn’t right, it at the least lets you discover route, all the very best!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments