user1960279
user1960279

Reputation: 514

how to use aspect ratio between subview and super view in xib files?

I am using Auto layout in xib files and not using Storyboard. I have added one UIImageView on self.view. I am trying to set the aspect ratio on UIImageView width and super view height. But when I drag the control from UIImageView( subview) to self.view (superview) to enable the aspect ratio option but I am surprised that aspect ratio option is not coming.

Only four option are coming

  1. Trailing space to container.
  2. Top space to container.
  3. Centre Horizontally in container .
  4. Centre vertically in container.

How to set the aspect ratio between subview and superview.

Thanks in Advance.

Upvotes: 1

Views: 4172

Answers (2)

vacawama
vacawama

Reputation: 154513

I'm not sure why XIBs don't allow you to set Aspect Ratio constraints at the top level. Here is a workaround:

  1. Add a view to the top level view.
  2. Pin its top, left, right, and bottom to the top level view's top, left, right, and bottom. Make all offsets 0.
  3. Now you can add your imageView to this view and use an Aspect Ratio constraint as you would expect.

Upvotes: 1

Benjamin Yi
Benjamin Yi

Reputation: 26

Emulating aspect-fit behaviour using AutoLayout constraints in Xcode 6

Excellent and simple explanation (with images!) that just might help you solve the problem. Hope this helps...

Upvotes: 1

Related Questions