IOS8 Auto-off with relative width / height

I need to autoplay a UIView storyboard using one UIControl. The UIControl should be centered, should have a 1: 1 aspect ratio, and should always use 90% of the width or height of the supervisor depending on the orientation of the device without cropping.

I added 4 constraints

  • Center Y Alignment - View - Supervisor
  • Center X Alignment - View - Supervisor
  • Equal Widths - View - Superstructure with multiplier set to 0.9
  • Aspect Ratio - View - View with a multiplier set to 1: 1

This setting works fine in portrait mode, but does not work in landscape mode. (See pictures below)

What limitation do I need to solve this problem. Do I need to change the restrictions if the device is rotated?

Portrait mode Portrait mode

Landscape mode is wrong Landscape mode - wrong

Landscape Mode - Layout. This is what I want. Landscape mode - ok

+3


source to share


2 answers


Here's how to do it:

  • Start with the four constraints listed above: Center X, Center Y, Equal Width (multiplier 0.9), and Aspect Ratio, all with a priority of 1000.
  • Change the priority of the equal latitudes to 750. This will allow the auto layout to ignore or change this constraint if needed.
  • Add Equal Heights (View - Superview) constraint, but instead of Equal, make it less than or equal with a multiplier of 0.9. Leave your priority at 1000.


Now that you are in a portrait, Auto Layout will be able to satisfy all of your limitations by making the red border still 90% wide. When you switch to terrain, the equal height constraint ensures that the red box doesn't grow more than 90% of the height, as this limit has priority of 1000, but auto-layout will also make the window as large as possible, trying to best use the equal width constraint.

+4


source


You have forced your view width and aspect ratio. So when you increase the width, auto-off will make your height grow to keep the aspect ratio of 1: 1. You must:

  • Set constraints for height and width as <= 0.9* Superview

    with priority 1000
  • Set limits for your height and width as == 0.9* Superview

    with priority 750


I haven't tried, but it should look better;)

+1


source







All Articles