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

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....

3      

Hey!

This happens because, even though you are runnning the app on a Mac, the app's target is still iOS. If you started your app for iOS and then checked the Mac checkbox in your project configs, your app is running a Catalyst version os the iOS app and therefore thinks it is an iOS app.

I think that to accomplish what you want you would have to make a new Mac-only target by going to the same place you checked the Mac checkbox and pressing the + at the bottom in the sidebar.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.