Skip to content
This repository has been archived by the owner on Sep 15, 2019. It is now read-only.

Commit

Permalink
activate
Browse files Browse the repository at this point in the history
  • Loading branch information
Satinder Singh committed May 1, 2017
1 parent 90f9c40 commit fd287ff
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions SnapLayout/Classes/SnapLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,66 @@ public extension UIView {
/// - height: Constant to apply from heightAnchor (if nil, not applied)
/// - centerX: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - centerY: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Note: width and height are not in respect to superview, but always to self
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil,
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: CGFloat? = nil,
centerY: CGFloat? = nil) -> SnapManager {
centerY: CGFloat? = nil, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
var snapManager = SnapManager(view: self)
var constraintList = [NSLayoutConstraint]()
defer {
if isActive {
NSLayoutConstraint.activate(constraintList)
if constraintList.count == 0 {
print("SnapLayout Error - No constraint was applied for view: \(String(describing: view))")
}
}
}
if let width = width {
snapManager.width = widthAnchor.constraint(equalToConstant: width)
snapManager.width?.isActive = true
let constraint = widthAnchor.constraint(equalToConstant: width)
snapManager.width = constraint
constraintList.append(constraint)
}
if let height = height {
snapManager.height = heightAnchor.constraint(equalToConstant: height)
snapManager.height?.isActive = true
let constraint = heightAnchor.constraint(equalToConstant: height)
snapManager.height = constraint
constraintList.append(constraint)
}
guard let view = view ?? superview else {
if snapManager.width == nil && snapManager.height == nil {
print("SnapLayout Error - No constraint was applied for view: \(String(describing: self))")
}
return snapManager
}
if let top = top {
snapManager.top = topAnchor.constraint(equalTo: view.topAnchor, constant: top)
snapManager.top?.isActive = true
let constraint = topAnchor.constraint(equalTo: view.topAnchor, constant: top)
snapManager.top = constraint
constraintList.append(constraint)
}
if let leading = leading {
snapManager.leading = leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: leading)
snapManager.leading?.isActive = true
let constraint = leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: leading)
snapManager.leading = constraint
constraintList.append(constraint)
}
if let bottom = bottom {
snapManager.bottom = view.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottom)
snapManager.bottom?.isActive = true
let constraint = view.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottom)
snapManager.bottom = constraint
constraintList.append(constraint)
}
if let trailing = trailing {
snapManager.trailing = view.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailing)
snapManager.trailing?.isActive = true
let constraint = view.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailing)
snapManager.trailing = constraint
constraintList.append(constraint)
}
if let centerX = centerX {
snapManager.centerX = centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: centerX)
snapManager.centerX?.isActive = true
let constraint = centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: centerX)
snapManager.centerX = constraint
constraintList.append(constraint)
}
if let centerY = centerY {
snapManager.centerY = centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: centerY)
snapManager.centerY?.isActive = true
}
let inActiveCount = [snapManager.width, snapManager.height, snapManager.top, snapManager.leading,
snapManager.bottom, snapManager.trailing, snapManager.centerX,
snapManager.centerY].filter ({ $0?.isActive == true }).count
if inActiveCount == 0 {
print("SnapLayout Error - No constraint was applied for view: \(String(describing: view))")
let constraint = centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: centerY)
snapManager.centerY = constraint
constraintList.append(constraint)
}
return snapManager
}
Expand Down

0 comments on commit fd287ff

Please sign in to comment.