J. Doe J. Doe - 1 year ago 91
Swift Question

How to make a UIImageView take up a fixed percent of the screen regardless of device?

Assume I have one big UIImageView that is set to a vector, and I want this ImageView to take up 90% of the screens width while maintaining it's aspect ratio, regardless of device, since it is a vector, resizing is not an issue. Height does not matter since I need it to just maintain it's aspect ratio, so long as it is 90% of the width of the screen. How can I do this in UIBuilder?

Answer Source

Here is how to make an image width 90% of its superview:

  1. Add the UIImageView to the Storyboard.
  2. control-drag from the imageView to the background view.
  3. Select Equal Widths from the pop-up.
  4. Change the multiplier for the constraint to 0.9.
  5. To set up the aspect ratio, control-drag from the UIImageView to itself and select Aspect Ratio from the pop-up. Set the multiplier value of the resulting constraint to the desired value. You can use a ratio such as 3:4 or a floating point value such as 0.75.
  6. In the Attributes Inspector set the Content Mode of the UIImageView to Scale To Fill, Aspect Fit, or Aspect Fill so that the UIImage fills the UIImageView.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download