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

mpospese opened this issue · comments

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

  1. struct Center which is an OptionSet (type Uint). It should have the following values:
    a. x = 1 << 0
    b. y = 1 << 1
    c. all = [.x, .y]
  2. method constrainCenter takes the following parameters:
    a. _ center: Center = .all
    b. to view2: Anchorable? = nil
    c. offset: UIOffset = .zero
    d. priority: UILayoutPriority = .required
    e. isActive: Bool = true
    This method creates centerX and/or centerY constraints using constrain anchor overrides (so .centerXAnchor and .centerYAnchor) and returns the one or two created constraints in a dictionary keyed by .centerX and/or .centerY, respectively. The result is discardable. When view2 == nil pass superview to constrain anchor (see otherView in UIView+constrainEdges.swift implementation). Generally refer to constrainEdges implementation as it will be similar only you are creating up to two constraints instead of up to four.
  3. Create a new XCTest file under Tests/YCoreUITests/Extensions/UIKit named UIView+constrainCenterTests.swift
    a. name the test UIViewContrainCenterTests and mark it final
    b. add three test cases to test creating only center x, only center y, and both.
  4. Ensure that all public interfaces are fully documented with documentation comments (including parameters and returns)

Usage:

// pin subview to center of its superview
view.pinCenter()
// pin subview to centerX of its superview
otherView.pinCenter(.x)