Trevor
Trevor

Reputation: 2457

Xcode center image while keeping square

Swift/xcode newbie here, be gentle.

I'm trying to set an image to be squared (around 160x160px) in the center of the screen on iphones and ipads.

Is there a way to set the coordinates per device. or any method around this?

constraints just scretch the image out.

Upvotes: 4

Views: 4072

Answers (2)

Orange
Orange

Reputation: 59

Or you can also use Aspect Ratio constraint if you need a larger or smaller image and making sure the image is always square, with Horizontally in Container and Vertically in Container Constraint set.

Upvotes: 2

Nick Winner
Nick Winner

Reputation: 261

I'm pretty new to Xcode, but I've recently learned about auto layout / constraints.

Select your image view and apply width and height constraints via the pin dialogue box (screenshot below), then open your alignment constraints and select center horizontally and center vertically. Then just apply your constraints and voila!

Like I said I'm still learning so if this is wrong, anybody please feel free to jump in.

enter image description here

enter image description here

Upvotes: 7

Related Questions