In my IOS app, I have a Location tab. There are UIPickerViews to choose a location and a UIButton to accept the chosen location. The layout puts the button too close to the picker views and it is too easy to change the picked item when pushing the button.
I tried to move the button further away from the picker view by specifying a 50 pixel distance to the nearest picker. I added a constraint on the button to do that. But the Xcode tool finds an opposing constraint that specifies a 0 distance. The error message at run time is:
"<NSLayoutConstraint:0x600002822da0 V:[UIPickerView:0x7fb0b2e1d5f0]-(50)-[UIButton:0x7fb0b2e1e7c0’Show on Map’] (active)>",
"<NSLayoutConstraint:0x6000028276b0 ‘UISV-spacing’ V:[UIPickerView:0x7fb0b2e1d5f0]-(0)-[UIButton:0x7fb0b2e1e7c0’Show on Map’] (active)>"
So this doesn’t work. The second constraint was not made by me.
How can I move the button further away than 0 pixels from the pickerview.
2
Answers
Instead of micro managing the stack view layout with complicated constraints, I would just try out the stack view distribution choices. Select the stack view in the attributes inspector and choose from the choices offered, find one you like and try it on several device simulators.
As you are seeing, UI design is much more involved than just adding components to a screen.
Via code, you can use
setCustomSpacing(_:after:)
to "fine-tune" the layout.You can also embed stack views within stack views, something like this:
I’ve exaggerated the vertical spacing to make the point.
View this on different iPhone sizes:
I don’t know if you’ve considered what to do with your layout for other Traits – such as on device rotation, or when running on an iPad… or multi-tasking on an iPad – but you might end up with trait variations to get something along these lines for the layout for wide-short format: