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 (type Uint). 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 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.
- 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.
- Ensure that all public interfaces are fully documented with documentation comments (including parameters and returns)
Create a new source file under
Sources/YCoreUI/Extensions/UIKitnamedUIView+constrainCenter.swiftPublicly extend
UIViewto add the following two items:Centerwhich is an OptionSet (typeUint). It should have the following values:a. x = 1 << 0
b. y = 1 << 1
c. all = [.x, .y]
constrainCentertakes the following parameters:a.
_ center: Center = .allb.
to view2: Anchorable? = nilc.
offset: UIOffset = .zerod.
priority: UILayoutPriority = .requirede.
isActive: Bool = trueThis method creates centerX and/or centerY constraints using
constrainanchor overrides (so.centerXAnchorand.centerYAnchor) and returns the one or two created constraints in a dictionary keyed by.centerXand/or.centerY, respectively. The result is discardable. Whenview2 == nilpasssuperviewto constrain anchor (seeotherViewinUIView+constrainEdges.swiftimplementation). Generally refer toconstrainEdgesimplementation as it will be similar only you are creating up to two constraints instead of up to four.Tests/YCoreUITests/Extensions/UIKitnamedUIView+constrainCenterTests.swifta. name the test
UIViewContrainCenterTestsand mark itfinalb. add three test cases to test creating only center x, only center y, and both.