Search code examples
objective-cxcodeswiftautolayoutconstraints

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)?

enter image description here


Solution

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

    Edit:

    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