SALE: Save 50% on all my books and bundles >>

SwiftUI on Mac, and PopUpButtonPickerStyle

Forums > SwiftUI

Hello, newly arrived here and a Swift/SwiftUI newbie (in fact its been years since I've dabbled in Cocoa at all... but have lots of dev experience in other realms). I'm building a SwiftUI app that has a fair it of data entry, and in a couple of places I'm using pickers -- the WheelPickerStyle() specifically. This has been working great in the simulator and on my iPad, but it doesn't work at all on the Mac build (it is drawn but is inert). The wheel isn't what I would want on the Mac anyhow, so I've tried using:

#if os(iOS)
        private let pickerStyle = WheelPickerStyle()
#else
        private let pickerStyle = PopUpButtonPickerStyle()
#endif

This seems to have no effect at all, however. On the Mac still drawn like a wheel picker and still inert. I can't even try just using the PopUpButtonPickerStyle directly but it won't build because that isn't available on iOS, and it doesn't seem to let me build only the Mac version.

Has anyone had any experience using these things? Google and StackOverflow haven't coughed up anything useful.


As I continue to try to find a reasonable solution I tried simply removing the ".pickerStyle(...)" view modifier. This is apparently different than any of the style options you can set, and it gives a navigation-like arrow and alternative pane. This would be a (barely) acceptable, albeit ugly, solution. Unfortunately it doesn't interact well with what I'm doing -- I have a VStack that contains a few views, 2 of which are HStacks. One of these HStacks contains 2 pickers, the other contains 4 (basically I have something akin to the data picker where you're assembling a kind of segmented value). This works great with the wheel picker on the iPad. Unfortunately when I try it without specifying the .pickerStyle(...), it does something bizarre -- touching it activates ALL of the pickers in the HStack, and it deactivates them all. So in one case the picker form slides in and back out twice, and in the other it does it four times. I've run into similar behaviour with the NavigationLink and Button, but the workaround/fix for those don't apply in this case.

Any suggestions?


Keep plugging away at it... I tried adding .buttonStyle(BorderlessButtonStyle()) to each picker (even though they aren't Buttons). This does change the behaviour, and I can now sort of select the value, but with way too much navigating back and forth between the form, the picker selection screen, and the previous form on the navigation stack! It is very strange and clearly buggier than hell.


Noticed that OSX 10.15.4 became available, so I updated to that and also switched my project to require it. After doing so, the wheel picker now works on the Mac version. Problem solved? Its a bit strange though because I'm still using the #if os() the I included above so it ought to be a popup menu instead of a wheel....

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building in-app subscriptions are hard. RevenueCat makes it simple. With their open source SDKs, you can painlessly implement subscriptions for your app in hours, not months.

Explore the docs to learn more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Snapthread is a casual video editor and slideshow maker that makes discovering, compiling and sharing your favorite memories effortless.

 
Unknown user

Not logged in

Log in