ColorPicker with empty label: horizontal alignment issue

The source of my problem I ran into (and only diagnosed due to the XCode “Debug View Hierarchy” tool) was being required to put an empty "" string for the native SwiftUI ColorPicker.

The problem itself is that this empty label actually takes up space in my view, thus stopping me from simply aligning my ColorPickers as such:

private var customColorPickers: some View {
    HStack(spacing: 10) {
        Spacer()
        ForEach(0..<(colorPalettes.last?.count ?? 0), id: .self) { customIndex in
            customColorPicker(index: customIndex)
        }
        Spacer()
    }
    .frame(alignment: .center)
}

For context, this is my customColorPicker definition:

private func customColorPicker(index: Int) -> some View {
    ColorPicker("",
        selection: Binding(
            get: {
                colorPalettes[3][index]
            },
            set: { newColor in
                
                colorPalettes[3][index] = newColor
                
                if (index == 0) {
                    widgetCustomColorPaletteFirstIndex = newColor.hex
                } else if (index == 1) {
                    widgetCustomColorPaletteSecondIndex = newColor.hex
                } else if (index == 2) {
                    widgetCustomColorPaletteThirdIndex = newColor.hex
                } else {
                    // do nothing, idk
                }
                sharedVars.colorPaletteIndex = 3
                widgetColorPaletteIndex = 3
                WidgetCenter.shared.reloadTimelines(ofKind: "QuoteDropletWidget")
            }
        ),
        supportsOpacity: true
    )
    .frame(width: 60, height: 60)
    .cornerRadius(8)
    .onChange(of: colorPalettes) { _ in
        WidgetCenter.shared.reloadTimelines(ofKind: "QuoteDropletWidget")
    }
}

You can see the empty view space below from the view hierarchy debugging tool (circled in red):

The solution I found was—full credit to this GitHub Gist user—from this code snippet he posted here (my implementation using his struct below):

import SwiftUI

@available(iOS 14.0, *)
public struct ColorPickerWithoutLabel: UIViewRepresentable {
    @Binding var selection: Color
    var supportsAlpha: Bool = true
    
    public init(selection: Binding<Color>, supportsAlpha: Bool = true) {
        self._selection = selection
        self.supportsAlpha = supportsAlpha
    }
    
    
    public func makeUIView(context: Context) -> UIColorWell {
        let well = UIColorWell()
        well.supportsAlpha = supportsAlpha
        return well
    }
    
    public func updateUIView(_ uiView: UIColorWell, context: Context) {
        uiView.selectedColor = UIColor(selection)
    }
}

My Code Before:

ColorPicker("",
        selection: Binding(
            get: {
                // irrelevant logic for Stackoverflow answer
            },
            set: { // irrelevant logic for Stackoverflow answer
            }
        ),
        supportsOpacity: true
    )
    // irrelevant modifiers for Stackoverflow answer

My Code After (implementing this ColorPickerWithoutLabel):

ColorPickerWithoutLabel(
        selection: Binding(
            get: {
                // irrelevant logic for Stackoverflow answer
            },
            set: { // irrelevant logic for Stackoverflow answer
            }
        ),
        supportsAlpha: true
    )
    // irrelevant modifiers for Stackoverflow answer

As you can tell, the only changes were to use ColorPickerWithoutLabel, instead of ColorPicker, of course + changing supportsOpacity to supportsAlpha + removing the empty label argument ("").

After this change, you can see in the View Hierarchy Debug tool that this view space is no longer being taken up by an empty label:

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật