2
votes

This is for XCode 5, which has sane autolayout generation.

If I drag, say, an UIImageView into a UIIView and want it to be centered in it's superview. I CTRL-drag the UIImageView onto it's superview and then shift-select Center Horizontally in Container as well as Center Vertically in Container.

This creates two constraints for where the constants are set to the current horizontal and vertical offset of the UIImageView. I've never had a case where this is what I wanted. I then have to manually select each contraints, change the constant to 0, then trigger the view to update its frame.

Is there a way, with some keybind or a preference option, to make it so that I can create constraints that have a zero constant, instead of the view's current offset?

1
I can't reproduce what you say you're seeing. What do you mean by "shift-select Center Horizontally in Container as well as Center Vertically in Container" -- what are you shift-selecting?rdelmar
@Hans Sjunnesson, do you use blue guidelines when drop down you UIImageView to center it within superview? There is "reset to suggested constraints" button which will set constraints correctly in that caseAzat
@rdelmar When you shift-click, you can select more than one constraint to be created.Hans Sjunnesson
@Azat If I position the view precisely where I need it, then yes, adding constraints gives me the correct offsets. However, I usually just drag a view into another. Then it's never perfectly centered.Hans Sjunnesson

1 Answers

0
votes

I haven't seen the case you're talking about where you get non-zero constraint values when you choose to add a centerX and centerY constraint -- that may be because we're adding the constraints in a different manner. The way I would add those constraints, would be to drag in the view, then click on the icon (second from left) at the bottom right of the canvas. Click on the check boxes for "horizontal center in container" and "vertical center in container", choose "items of new constraints" from the pull down control at the bottom of that window, and click "add constraints".

enter image description here