How to remove internal padding to the right of SwiftUI Picker - Stack Overflow
I'm curious, have you noticed how Pickers have different internal padding when in a Form vs not? Of course, there are many controls that have special looks within the context of a Form. But I would like to have a Picker that isn't in a Form, and I'm trying to line up the Picker button with other controls, and its internal padding is getting in the way.
Here's some code to illustrate:
enum ColorStyle: CaseIterable, Identifiable, CustomStringConvertible {
case rgb, hsv
var id: Self { self } // to make it Identifiable
var description: String { // to make it CustomStringConvertible
switch self {
case .rgb:
return "RGB"
case .hsv:
return "HSV"
}
}
}
struct FormComparison: View {
@State private var colorStyle = ColorStyle.rgb
var colorSource: some View {
HStack {
Text("Color Type")
Spacer()
Picker("Color Type", selection: $colorStyle) {
ForEach(ColorStyle.allCases) { style in
Text(String(describing: style))
}
}
.pickerStyle(.menu)
.labelsHidden()
.border(.green)
}
}
var colorSwatch: some View {
HStack {
Text("Color")
Spacer()
Color.red.frame(width: 100, height: 25)
}
}
var body: some View {
VStack {
Form {
colorSwatch
colorSource
}
VStack {
colorSwatch
colorSource
Spacer()
}
.padding()
.background(.blue.opacity(0.15))
.padding()
}
.frame(height: 400)
}
}
Here's the result:
In the bottom version, which is not in a Form, the Picker is adding some padding to the right of the selector button (clearly visible from the green border I've added), and that's making the non-Form UI look janky.
I'm curious, have you noticed how Pickers have different internal padding when in a Form vs not? Of course, there are many controls that have special looks within the context of a Form. But I would like to have a Picker that isn't in a Form, and I'm trying to line up the Picker button with other controls, and its internal padding is getting in the way.
Here's some code to illustrate:
enum ColorStyle: CaseIterable, Identifiable, CustomStringConvertible {
case rgb, hsv
var id: Self { self } // to make it Identifiable
var description: String { // to make it CustomStringConvertible
switch self {
case .rgb:
return "RGB"
case .hsv:
return "HSV"
}
}
}
struct FormComparison: View {
@State private var colorStyle = ColorStyle.rgb
var colorSource: some View {
HStack {
Text("Color Type")
Spacer()
Picker("Color Type", selection: $colorStyle) {
ForEach(ColorStyle.allCases) { style in
Text(String(describing: style))
}
}
.pickerStyle(.menu)
.labelsHidden()
.border(.green)
}
}
var colorSwatch: some View {
HStack {
Text("Color")
Spacer()
Color.red.frame(width: 100, height: 25)
}
}
var body: some View {
VStack {
Form {
colorSwatch
colorSource
}
VStack {
colorSwatch
colorSource
Spacer()
}
.padding()
.background(.blue.opacity(0.15))
.padding()
}
.frame(height: 400)
}
}
Here's the result:
In the bottom version, which is not in a Form, the Picker is adding some padding to the right of the selector button (clearly visible from the green border I've added), and that's making the non-Form UI look janky.
Share Improve this question asked 12 hours ago Curious JorgeCurious Jorge 1,1994 silver badges30 bronze badges 1 |1 Answer
Reset to default 0You can put it in a Menu
with a custom label:
Menu {
Picker("Color Type", selection: $colorStyle) {
ForEach(ColorStyle.allCases) { style in
Text(String(describing: style))
}
}
} label: {
HStack(spacing: 5) {
Text(colorStyle.description)
Image(systemName: "chevron.up.chevron.down")
}
}
.labelsHidden()
.border(.green)
- 谷歌回应欧盟反垄断指控:安卓利于竞争和消费者
- 50公里的传奇 Ubiquiti Networks
- reactjs - CORS issue from React to Flask - Stack Overflow
- Why does this typecheck in Lean? - Stack Overflow
- TypeScript Error (ts2345) when trying to collect property names in array - Stack Overflow
- php - Yii2 ActiveForm Model doesn't exist - Stack Overflow
- ST_CONTAINS() giving FALSE even if Point lies within polygon. google-bigquery - Stack Overflow
- Best practices for Python imports in a production project: handling relativeabsolute imports across different directories and te
- typescript - Issues with generic type narrowing on merged records - Stack Overflow
- python - Assist LSP with dynamically imported methods - Stack Overflow
- rcpp - C++ AVX2 custom functions (e.g., "exp") not working on Windows (but work on Linux) - Stack Overflow
- c# - Querying CosmosDB_SQL from ASP.NET Core 8 Web API to return Single value - Stack Overflow
- python - Azure Cognitive Vector search query and index creation - Stack Overflow
- sql - Rolling sum that resets at a flag - Stack Overflow
- dolphindb - Whitepaper error: “cannot recognize the token b” - Stack Overflow
- pine script - Is there a way to incorporate dynamic commission fees in the TradingView Strategy Tester? - Stack Overflow
- indexing - mysql 5.7 why this sql not using index_merge,it using a full table scan - Stack Overflow
.offset(x: 10)
just before.pickerStyle(.menu)
– workingdog support Ukraine Commented 12 hours ago