UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Horizontal Picker Squeezes items

Forums > Swift

Since there is no horizontal Picker built into SwiftUI*, how are you doing this? Are you putting everything into an HStack? That would cause the items to be squeezed as you describe. I think you would need to create your own custom horizontal Picker, maybe using a ScrollView to display the items and a GeometryReader to achieve effects like dimming the items near the edges and so forth.

I don't know if something could be done with a custom PickerStyle, but maybe. I haven't had a need to play around with that yet.

  • Well, I suppose the SegmentedPickerStyle creates a horizontal Picker, but it's not intended to scroll so if you put a lot of items into it, everything will get smooshed. No way around that, really.

   

You would have to use an onTapGesture handler.

A very simple example:

import SwiftUI

struct HScroller: View {
    @State private var bgndColor = Color.white

    var body: some View {
        ZStack {
            bgndColor
                .edgesIgnoringSafeArea(.all)
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(0..<10, id: \.self) { idx in
                        ZStack {
                            Circle()
                                .fill(self.bgndColor)
                                .colorInvert()
                                .frame(width: 50, height: 50)
                                .padding()
                            Text("\(idx)")
                                .foregroundColor(.white)
                                .bold()
                        }
                        .onTapGesture {
                            self.bgndColor = Color(red: Double.random(in: 0...1.0), green: Double.random(in: 0...1.0), blue: Double.random(in: 0...1.0))
                        }
                    }
                }
            }
        }
    }
}

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.