iOS – proportional spacing with autolayout
I’m trying to create a perfectly to-scale view with interface builder. So far, everything is good. I’m scaling the font, buttons, etc programmatically.. the only problem is the constraints (the spacing) between elements which remains the same. I want to avoid creating outlets for the spacing constraints because it seems messy. I want the spacing between elements to remain proportional as i stretch the element. right now everything is aligned based on the left side of the screen. if i stretch my view, everything stays left aligned. I want it to all stretch apart proportionally. How can I accomplish this (preferably using interface builder)?
- iOS: Auto Layout: Two buttons side-by-side when there's enough space, otherwise on top of one another
- Where is the Xcode checkbox “Translates Autoresizing Mask Into Constraints”
- How can I add padding to the intrinsic content size of UILabel?
- Can I use autolayout to provide different constraints for landscape and portrait orientations?
- UITableViewHeaderFooterView subclass with auto layout and section reloading won't work well together
- How can I use constraints AutoLayout with to specify a different layout on landscape vs. portrait without using code?
2 Solutions Collect From Internet About “iOS – proportional spacing with autolayout”
Note – this is kind of a sketchy way of doing it.
I had a similar issue and resolved it by adding dummy views between my objects to represent the spacing between them. You can constrain the dummy views to scale proportionally to the rest of your views, which will make the spacing between your objects scale properly with the overall size. I set the hidden property of my dummy views so they wouldn’t show up (note – they still get laid out properly when they are hidden).
Hope that helps.
This method is imperfect (you have to clutter IB with extraneous views), but like @sha says it seems to be the only way to get it done.
It turns out that other people have been giving similar advice. I came across these references which might be helpful:
AutoLayout to keep view sizes proportional
AutoLayout: layout consistency with proportional element spacing with 3.5" and 4" screens
Dummy views??? You must be kidding me. And if that’s what Apple recommends – then I don’t have much respect for them.
What you’re clearly looking for is the central horizontal (Center X Alignment) constraint and then you just need to play with the multiplier while keeping the constant at 0:
The multiplier goes from 0-2
1 will place the center of the subview at the center of the superview. 0 will place the center of the subview at the left edge of the superview. 2 will place the center of the subview at the right edge of the superview. 0.5 will place the center of the subview at 25% through the superview. 1.5 will place the center of the subview at 75% through the superview.
Get rid of all those horizontal space constraints. And always think about multipliers – the constants of the constraints should always be set to 0.
Apple is obsessed with native resolution – they don’t want your design to be scaled across different screen sizes. Screw them. Scaled resolution all the way.
- What is 'llvm-gcc-4.2' doing in '/usr' on my Yosemite system?
- How to track multiple touches
- NSDate isEqualToDate: not working – does it look at seconds and split seconds?
- Scroll to top of UITableView by tapping status bar
- Keep autolayout constraints active status on device rotation
- How to put text over node in scenekit?
- CGImage from NSURL works but not from UIImage
- How do I restrict UITextField to English only? (Stopping Chinese pinyin input)
- Convert NSString to ASCII Binary Equivilent (and then back to an NSString again)
- UIWebView: HTML5 audio pauses in iOS 6 when app enters background
- How do I adjust the anchor point of a CALayer, when Auto Layout is being used?
- Xcode ARC conversion tool issue
- How to retrict date on UIDatePickerView?
- How to create Entitlement.plist file in xcode 5?
- How to downgrade XCode to previous version?