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 `constrainSize` auto layout extension to UIView

mpospese opened this issue · comments

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

  1. constrainSize takes parameters _ size: CGSize, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required, isActive: Bool = true and returns a dictionary [NSLayoutConstraint.Attribute: NSLayoutConstraint]
    a. creates width and height constraints using constrain anchor overrides (so .widthAnchor and .heightAnchor)
    b. returns the two created constraints in a dictionary keyed by .width and .height, respectively.
    c. result is discardable
  2. constrainSize takes parameters width: CGFloat, height: CGFloat, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required, isActive: Bool = true and returns same dictionary as 1.
    a. creates a CGSize using width and height and then calls the override from 1.
  3. constrainSize takes parameter _ dimension: CGFloat, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required, isActive: Bool = true and returns same dictionary as 1.
    a. creates a CGSize where width and height both equal dimension and then calls the override from 1.
  4. Create a new XCTest file under Tests/YCoreUITests/Extensions/UIKit named UIView+constrainSizeTests.swift
    a. name the test UIViewContrainSizeTests and mark it final
    b. add test case for each of the three methods created above.
  5. Ensure that the three new methods are fully documented with documentation comments (including parameters and returns)

Usage:

let mySize: CGSize

imageView.pinSize(mySize)
otherView.pinSize(width: 320, height: 480)
tinyButton.pinSize(44) // 44 x 44