Add `constrainCenter` auto layout extension to UIView
mpospese opened this issue · comments
Mark Pospesel commented
Create a new source file under Sources/YCoreUI/Extensions/UIKit
named UIView+constrainCenter.swift
Publicly extend UIView
to add the following two items:
- struct
Center
which is an OptionSet (typeUint
). It should have the following values:
a. x = 1 << 0
b. y = 1 << 1
c. all = [.x, .y] - 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 usingconstrain
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. Whenview2 == nil
passsuperview
to constrain anchor (seeotherView
inUIView+constrainEdges.swift
implementation). Generally refer toconstrainEdges
implementation as it will be similar only you are creating up to two constraints instead of up to four. - Create a new XCTest file under
Tests/YCoreUITests/Extensions/UIKit
namedUIView+constrainCenterTests.swift
a. name the testUIViewContrainCenterTests
and mark itfinal
b. add three test cases to test creating only center x, only center y, and both. - Ensure that all public interfaces are fully documented with documentation comments (including parameters and returns)
Mark Pospesel commented
Usage:
// pin subview to center of its superview
view.pinCenter()
// pin subview to centerX of its superview
otherView.pinCenter(.x)