HomeiOS Developmentios - SwiftUI Picker difficulty with Rectangle() as a substitute of Textual...

ios – SwiftUI Picker difficulty with Rectangle() as a substitute of Textual content()


I need to use a SwiftUI Picker to pick out completely different colours. As a substitute of utilizing Textual content() for every merchandise in a picker, I need to use a Rectangle() to visually present the colour, however SwiftUI does not prefer it and reveals me a bunch of clean areas. Listed below are my implementations:

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

and

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


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

Thanks prematurely for any steering!!

I learn someplace that the Picker expects the choice component to adapt to Hashable, so I made the ColorSquare struct for that reason.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments