Recipe: Setting Aspect Ratio
Constraint multipliers, the m of the y = m * x + b equation, can help set aspect ratios for your views. Recipe 5-4 demonstrates how to do this by relating a view’s height (y) to its width (x) and setting the m value to the aspect. Recipe 5-4 builds an NSLayoutRelationEqual relationship between the width and height of a view, using the aspect ratio as the multiplier.
The recipe applies its aspect updates by managing a fixed constraint, which it stores locally in an NSLayoutConstraint variable called aspectConstraint. Each time the user toggles the aspect from 16:9 to 4:3 or back, this recipe removes the previous constraint and creates and then stores another one. It builds this new constraint by setting the appropriate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access