Added Center X & Y constraints and modified code based on constraints

This commit is contained in:
Krishna Kishore Bandaru 2024-03-20 16:50:47 +05:30
parent 2d7bdf9a8f
commit 86e91c3ac7
3 changed files with 174 additions and 18 deletions

View File

@ -92,34 +92,38 @@ open class Pagination: View {
open override func initialSetup() {
super.initialSetup()
addSubview(containerView)
containerView
.pinTop()
.pinBottom()
containerView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).activate()
trailingAnchor.constraint(greaterThanOrEqualTo: containerView.trailingAnchor).activate()
containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
containerView.widthAnchor.constraint(equalToConstant: maxWidth).activate()
containerView.heightAnchor.constraint(equalToConstant: 44).activate()
collectionContainerView.addSubview(collectionView)
containerView.addSubview(previousButton)
containerView.addSubview(collectionContainerView)
containerView.addSubview(nextButton)
collectionContainerView.addSubview(collectionView)
addSubview(containerView)
containerView
.pinTop()
.pinBottom()
.pinLeadingGreaterThanOrEqualTo()
.pinTrailingLessThanOrEqualTo()
.pinCenterX()
.width(maxWidth)
.height(44)
previousButton
.pinTop()
.pinBottom()
.pinLeading()
.pinTrailingGreaterThanOrEqualTo(collectionContainerView.leadingAnchor)
previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionContainerView.leadingAnchor).activate()
collectionContainerView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate()
collectionContainerView
.pinTrailingGreaterThanOrEqualTo(nextButton.leadingAnchor)
.pinTop()
.pinBottom()
collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate()
collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate()
collectionView.centerXAnchor.constraint(equalTo: collectionContainerView.centerXAnchor).activate()
collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92)
collectionViewWidthAnchor?.activate()
collectionView
.height(VDSLayout.Spacing.space4X.value)
.pinCenterY()
.pinCenterX()
collectionViewWidthAnchor = collectionView.width(constant: 92)
nextButton
.pinTop()

View File

@ -48,7 +48,7 @@ final class PaginationCellItem: UICollectionViewCell {
contentView.addSubview(containerView)
containerView.pinToSuperView()
indexLabel.pinToSuperView()
indexLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: VDSLayout.Spacing.space5X.value).activate()
indexLabel.widthGreaterThanEqualTo(VDSLayout.Spacing.space5X.value)
contentView.backgroundColor = .clear
containerView.backgroundColor = .clear
indexLabel.backgroundColor = .clear

View File

@ -478,7 +478,159 @@ extension LayoutConstraintable {
}
}
//--------------------------------------------------
// MARK: - Center X Constraints
//--------------------------------------------------
extension LayoutConstraintable {
@discardableResult
/// Adds a centerXAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinCenterX(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterX(nil, constant)
}
@discardableResult
/// Adds a centerXAnchor to a specific XAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the centerXAnchor.
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterX(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterX(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerXAnchor to a specific XAxisAnchor passed in using a lessThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the centerXAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterXLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterXLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerXAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the centerXAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterXGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinCenterXGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerXAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the centerXAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterX(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor
guard let found else { return nil }
return centerXAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
@discardableResult
/// Adds a centerXAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the centerXAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterXLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor
guard let found else { return nil }
return centerXAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
@discardableResult
/// Adds a centerXAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the centerXAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterXGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor
guard let found else { return nil }
return centerXAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
}
//--------------------------------------------------
// MARK: - Center Y Constraints
//--------------------------------------------------
extension LayoutConstraintable {
@discardableResult
/// Adds a centerYAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinCenterY(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterY(nil, constant)
}
@discardableResult
/// Adds a centerYAnchor to a specific YAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the centerYAnchor.
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterY(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterY(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerYAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the centerYAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterYLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self {
pinCenterYLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerYAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the centerYAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinCenterYGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinCenterXGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a centerYAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the centerYAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterY(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor
guard let found else { return nil }
return centerYAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
@discardableResult
/// Adds a centerYAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the centerYAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterYLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor
guard let found else { return nil }
return centerYAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
@discardableResult
/// Adds a centerYAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the centerYAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinCenterYGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor
guard let found else { return nil }
return centerYAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true }
}
}
//--------------------------------------------------
// MARK: - Implementations
//--------------------------------------------------