codeandtheory / YCoreUI

Core components for iOS and tvOS to accelerate building user interfaces in code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add `constrainAspectRatio` auto layout extension method to UIView

mpospese opened this issue · comments

Create a new source file under Sources/YCoreUI/Extensions/UIKit named UIView+constrainAspectRatio.swift
Publicly extend UIView to add the following method:

  1. constrainAspectRatio takes parameters
    a. _ ratio: CGFloat
    b. offset: CGFloat = 0
    b. relatedBy relation: NSLayoutConstraint.Relation = .equal
    c, priority: UILayoutPriority = .required
    d, isActive: Bool = true
  2. it creates a constraint on the receiving view, constraining its widthAnchor to its heightAnchor using ratio as the multiplier and optional offset as the constant.
  3. it returns the single created NSLayoutConstraint but it is @discardableResult
  4. Create a new XCTest file under Tests/YCoreUITests/Extensions/UIKit named UIView+constrainAspectRatioTests.swift
    a. name the test UIViewContrainAspectRatioTests and mark it final
    b. add test case for the method created above.
  5. Ensure that the new method is fully documented with documentation comments (including parameters and returns)
  6. Add file comment example of how to set the aspect ratio of an image view (see UIView+constrainAnchor.swift Use-case examples)
  7. Add same example to README
  8. Add same example to Notion page: https://www.notion.so/ymedialabs/Y-CoreUI-882275f4b88a408dba83f93539433d44