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 ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.