Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
KWANG HYOUN KIM committed Apr 14, 2019
2 parents eb11424 + 70c10ed commit c503d0b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ $ brew install carthage
To integrate Alamofire into your Xcode project using Carthage, specify it in your `Cartfile`:

```ogdl
github "pisces/SimpleLayout" ~> 1.0.2
github "pisces/SimpleLayout" ~> 1.0.3
```

Run `carthage update` to build the framework and drag the built `SimpleLayout.framework` into your Xcode project.
Expand Down
2 changes: 1 addition & 1 deletion SimpleLayout-Swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SimpleLayout-Swift'
s.version = '1.0.2'
s.version = '1.0.3'
s.summary = 'SimpleLayout helps you to using auto layout very easily'
s.description = <<-DESC
TODO: Add long description of the pod here.
Expand Down
105 changes: 67 additions & 38 deletions SimpleLayout/Classes/SimpleLayoutObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// - Support iOS 11 and higher
// Modified by pisces on 4/12/2018.
// - Add priority to NSLayoutConstraint
// Modified by pisces on 4/6/2019.
// - Remove constraint if it is equal to origin when set the new constraint.
// Copyright © 2016 Steve Kim. All rights reserved.
//
//
Expand Down Expand Up @@ -38,9 +40,12 @@ public class SimpleLayoutObject: NSObject {

@discardableResult
public func bottom(by target: Any? = nil, priority: UILayoutPriority = .required, attribute: NSLayoutAttribute = .bottom, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeBottom()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: item, attribute: attribute, multiplier: multiplier, constant: constant)

constraints.bottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: toItem(with: target), attribute: attribute, multiplier: multiplier, constant: constant)
removeBottom(with: new)

constraints.bottom = new
constraints.bottom?.priority = priority
constraints.bottom?.isActive = true
return self
Expand All @@ -57,27 +62,36 @@ public class SimpleLayoutObject: NSObject {
}
@discardableResult
public func centerX(by target: Any? = nil, priority: UILayoutPriority = .required, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeHorizontalConstraints()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: item, attribute: .centerX, multiplier: multiplier, constant: constant)

removeHorizontalConstraints(with: new)

constraints.centerX = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: toItem(with: target), attribute: .centerX, multiplier: multiplier, constant: constant)
constraints.centerX = new
constraints.centerX?.priority = priority
constraints.centerX?.isActive = true
return self
}
@discardableResult
public func centerY(by target: Any? = nil, priority: UILayoutPriority = .required, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeVerticalConstraints()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: item, attribute: .centerY, multiplier: multiplier, constant: constant)

removeVerticalConstraints(with: new)

constraints.centerY = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: toItem(with: target), attribute: .centerY, multiplier: multiplier, constant: constant)
constraints.centerY = new
constraints.centerY?.priority = priority
constraints.centerY?.isActive = true
return self
}
@discardableResult
public func trailing(by target: Any? = nil, priority: UILayoutPriority = .required, attribute: NSLayoutAttribute = .trailing, relation: NSLayoutRelation = .equal, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeTrailing()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: relation, toItem: item, attribute: attribute, multiplier: multiplier, constant: constant)

removeTrailing(with: new)

constraints.trailing = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: relation, toItem: toItem(with: target), attribute: attribute, multiplier: multiplier, constant: constant)
constraints.trailing = new
constraints.trailing?.priority = priority
constraints.trailing?.isActive = true
return self
Expand Down Expand Up @@ -118,18 +132,24 @@ public class SimpleLayoutObject: NSObject {
}
@discardableResult
public func leading(by target: Any? = nil, priority: UILayoutPriority = .required, attribute: NSLayoutAttribute = .leading, relation: NSLayoutRelation = .equal, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeLeading()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .leading, relatedBy: relation, toItem: item, attribute: attribute, multiplier: multiplier, constant: constant)

constraints.leading = NSLayoutConstraint(item: view, attribute: .leading, relatedBy: relation, toItem: toItem(with: target), attribute: attribute, multiplier: multiplier, constant: constant)
removeLeading(with: new)

constraints.leading = new
constraints.leading?.priority = priority
constraints.leading?.isActive = true
return self
}
@discardableResult
public func top(by target: Any? = nil, priority: UILayoutPriority = .required, attribute: NSLayoutAttribute = .top, multiplier: CGFloat = 1.0, _ constant: CGFloat = 0) -> SimpleLayoutObject {
removeTop()
let item = toItem(with: target)
let new = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: item, attribute: attribute, multiplier: multiplier, constant: constant)

removeTop(with: new)

constraints.top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: toItem(with: target), attribute: attribute, multiplier: multiplier, constant: constant)
constraints.top = new
constraints.top?.priority = priority
constraints.top?.isActive = true
return self
Expand All @@ -154,65 +174,74 @@ public class SimpleLayoutObject: NSObject {
// MARK: - Remove constraint

@discardableResult
public func removeBottom() -> SimpleLayoutObject {
if let bottom = constraints.bottom {
view.superview?.removeConstraint(bottom)
public func removeBottom(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.bottom, new: new) {
constraints.bottom = nil
}
return self
}
@discardableResult
public func removeCenterX() -> SimpleLayoutObject {
if let centerX = constraints.centerX {
view.superview?.removeConstraint(centerX)
public func removeCenterX(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.centerX, new: new) {
constraints.centerX = nil
}
return self
}
@discardableResult
public func removeCenterY() -> SimpleLayoutObject {
if let centerY = constraints.centerY {
view.superview?.removeConstraint(centerY)
public func removeCenterY(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.centerY, new: new) {
constraints.centerY = nil
}
return self
}
@discardableResult
public func removeLeading() -> SimpleLayoutObject {
if let leading = constraints.leading {
view.superview?.removeConstraint(leading)
public func removeLeading(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.leading, new: new) {
constraints.leading = nil
}
return self
}
@discardableResult
public func removeTop() -> SimpleLayoutObject {
if let top = constraints.top {
view.superview?.removeConstraint(top)
public func removeTop(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.top, new: new) {
constraints.top = nil
}
return self
}
@discardableResult
public func removeTrailing() -> SimpleLayoutObject {
if let trailing = constraints.trailing {
view.superview?.removeConstraint(trailing)
public func removeTrailing(with new: NSLayoutConstraint? = nil) -> SimpleLayoutObject {
if removeConstraint(constraints.trailing, new: new) {
constraints.trailing = nil
}
return self
}

// MARK: - Private methods

private func removeHorizontalConstraints() {
removeLeading()
removeTrailing()
removeCenterX()
private func isRemovableConstraint(_ constraint: NSLayoutConstraint, new: NSLayoutConstraint?) -> Bool {
guard let new = new, constraint.firstAttribute != new.firstAttribute else {
return true
}
let originItem = (constraint.secondItem as? UIView) ?? (constraint.secondItem as? NSLayoutConstraint)
let newItem = (new.secondItem as? UIView) ?? (new.secondItem as? NSLayoutConstraint)
return originItem == newItem
}
private func removeConstraint(_ constraint: NSLayoutConstraint?, new: NSLayoutConstraint?) -> Bool {
guard let constraint = constraint, isRemovableConstraint(constraint, new: new) else {
return false
}
view.superview?.removeConstraint(constraint)
return true
}
private func removeHorizontalConstraints(with new: NSLayoutConstraint? = nil) {
removeLeading(with: new)
removeTrailing(with: new)
removeCenterX(with: new)
}
private func removeVerticalConstraints() {
removeTop()
removeCenterY()
removeBottom()
private func removeVerticalConstraints(with new: NSLayoutConstraint? = nil) {
removeTop(with: new)
removeCenterY(with: new)
removeBottom(with: new)
}
private func toItem(with target: Any?) -> Any? {
return target != nil ? target! : view.superview as Any
Expand Down

0 comments on commit c503d0b

Please sign in to comment.