'SwiftUI Custom Picker Label Not Rendering
After updating to iOS 15 and Xcode 13, my picker in my app is no longer showing a custom label. Running the app on an iOS 14 device, the pickers render fine.
This is the code snippet that is currently implemented and the screenshot is what it currently looks like in the simulator on iOS 15.
@State var selectedNumber: Int = 0
var body: some View {
Picker(selection: $selectedNumber, label: customLabel) {
ForEach(0..<10) {
Text("\($0)")
}
}
}
var customLabel: some View {
HStack {
Image(systemName: "paperplane")
Text(String(selectedNumber))
Spacer()
Text("⌵")
.offset(y: -4)
}
.foregroundColor(.white)
.font(.title)
.padding()
.frame(height: 32)
.background(Color.blue)
.cornerRadius(16)
}
Solution 1:[1]
The answer @Adam provided worked. Below is what I did to fix it in case someone else stumbles on problem.
@State var selectedNumber: Int = 0
var body: some View {
Menu {
Picker(selection: $selectedNumber, label: EmptyView()) {
ForEach(0..<10) {
Text("\($0)")
}
}
} label: {
customLabel
}
}
var customLabel: some View {
HStack {
Image(systemName: "paperplane")
Text(String(selectedNumber))
Spacer()
Text("?")
.offset(y: -4)
}
.foregroundColor(.white)
.font(.title)
.padding()
.frame(height: 32)
.background(Color.blue)
.cornerRadius(16)
}
Solution 2:[2]
Your can Use Menu Options as label
Menu("Drop Your Label Here") {
Picker("Please choose a color", selection: $selectedColor)
{
ForEach(colors, id: \.self) {
Text($0)
}
}
}
//.foregroundColor(Color.init("Red"))
.foregroundColor(Color.red)
}.frame(width: 300, height: 60)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pkamb |
Solution 2 | pkamb |