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

Commit

Permalink
[FEATURE] Support Priority
Browse files Browse the repository at this point in the history
- SnapLayout now supports priority with all snaps!
- Added test for priority
- Updated Example project priority cell with update
  • Loading branch information
Satinder Singh committed Jun 7, 2017
1 parent f7d1333 commit 7b3ea6e
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SnapLayout (1.6.2)
- SnapLayout (1.8.0)

DEPENDENCIES:
- SnapLayout (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SnapLayout: 387502e5b6acbd2f12308fab94425907a87866c1
SnapLayout: bef399cd9d9cf1959e249edd58eadc60836862dc

PODFILE CHECKSUM: 15badb46d75d789f8e26d30c1acb2aa5249c4ced

Expand Down
7 changes: 4 additions & 3 deletions Example/Pods/Local Podspecs/SnapLayout.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion Example/SnapLayout/AdjacentHorizontalTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal final class AdjacentHorizontalTableViewCell: TitleTableViewCell {
fileprivate func setupLabels() {
let leadingLabel = Style.subtitleLabel(text: "Leading Label")
contentView.addSubview(leadingLabel)
leadingLabel.snap(constants: leadingLabelConfig)
leadingLabel.snap(config: leadingLabelConfig)

let trailingLabel = Style.subtitleLabel(text: "Trailing Label")
contentView.addSubview(trailingLabel)
Expand Down
12 changes: 8 additions & 4 deletions Example/SnapLayout/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand All @@ -24,7 +28,7 @@
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
Expand Down
2 changes: 2 additions & 0 deletions Example/SnapLayout/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
</dict>
</plist>
10 changes: 4 additions & 6 deletions Example/SnapLayout/PriorityTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import SnapLayout

/// Displays rectangle view with a constraint whose priority is updated
class PriorityTableViewCell: TitleTableViewCell {
Expand All @@ -27,14 +28,11 @@ class PriorityTableViewCell: TitleTableViewCell {
contentView.addSubview(rectangleView)

// Snaps square view by pinning to all 4 sides (activated constraints)
// Then creates an inactive height constraint whose priority is updated before being activated through `squareViewSnapManager`
// Then creates a height constraint whose priority is updated and then activated
// Updating height priority to avoid following runtime warning
// http://stackoverflow.com/questions/28410309/strange-uiview-encapsulated-layout-height-error
let squareViewSnapManager = rectangleView
.snap(top: 48, leading: 16, bottom: 16, trailing: 16)
.snap(height: 40, isActive: false)
squareViewSnapManager.height?.priority = 999
squareViewSnapManager.height?.isActive = true
rectangleView.snap(top: 48, leading: 16, bottom: 16, trailing: 16)
.snap(height: 40, priority: UILayoutPriorityDefaultHigh)

let circleView = UIView()
circleView.backgroundColor = .green
Expand Down
2 changes: 1 addition & 1 deletion Example/SnapLayout/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal final class ViewController: UIViewController {
tableView.allowsSelection = false
tableView.tableFooterView = UIView(frame: .zero)
view.addSubview(tableView)
tableView.snap(constants: .zero) // snaps top, leading, bottom, and trailing
tableView.snap(config: .zero) // snaps top, leading, bottom, and trailing
}

}
Expand Down
12 changes: 12 additions & 0 deletions Example/SnapLayout_ExampleTests/SnapLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,17 @@ class SnapLayoutTests: BaseTestCase {
XCTAssertEqual(snapManagerInActive.top!.constant, topConstant)
XCTAssertEqual(snapManagerInActive.top!.multiplier, 1.0)
}

/// Tests priority for a snap
func testPriority() {
let snapManagerDefaultPriority = childView.snapHeight(to: childView2, multiplier: 0.5)
XCTAssertEqual(snapManagerDefaultPriority.height!.priority, LayoutPriorityRequired)

let snapManagerHighPriority = childView.snapWidth(to: childView2, priority: UILayoutPriorityDefaultHigh)
XCTAssertEqual(snapManagerHighPriority.width!.priority, UILayoutPriorityDefaultHigh)

let snapManagerLowPriority = childView.snap(top: 100, priority: UILayoutPriorityDefaultLow)
XCTAssertEqual(snapManagerLowPriority.top!.priority, UILayoutPriorityDefaultLow)
}

}
6 changes: 3 additions & 3 deletions Example/SnapLayout_ExampleTests/SnapManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SnapManagerTests: BaseTestCase {
let containerView = UIView()
containerView.addSubview(containerViewSubview)
containerView.addSubview(otherView)
let snapManager = containerViewSubview.snap(to: otherView, constants: .zero)
let snapManager = containerViewSubview.snap(to: otherView, config: .zero)
XCTAssertEqual(containerViewSubview.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertNotNil(snapManager.top)
XCTAssertEqual(snapManager.top!.constant, 0)
Expand Down Expand Up @@ -124,7 +124,7 @@ class SnapManagerTests: BaseTestCase {
height: heightConstant,
centerX: centerXConstant,
centerY: centerYConstant)
let snapManager = childView.snap(constants: snapConfig)
let snapManager = childView.snap(config: snapConfig)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertNotNil(snapManager.top)
XCTAssertEqual(snapManager.top!.constant, topConstant)
Expand Down Expand Up @@ -162,7 +162,7 @@ class SnapManagerTests: BaseTestCase {
width: widthConstant,
height: heightConstant)
let centerConfig = SnapConfig(centerX: centerXConstant, centerY: centerYConstant)
let snapManager = childView.snap(constants: snapConfig)
let snapManager = childView.snap(config: snapConfig)
.snap(config: centerConfig)
XCTAssertNotNil(snapManager.top)
XCTAssertEqual(snapManager.top!.constant, topConstant)
Expand Down
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ github "sp71/SnapLayout"

### [`Methods`](SnapLayout/Classes/SnapLayout.swift)
```swift
func snap(to view: View? = 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, isActive: Bool = true) -> SnapManager
func snap(to view: View? = nil, constants: SnapConfig, isActive: Bool = true) -> SnapManager
func snapWidth(to view: View, multiplier: CGFloat = 1, isActive: Bool = true) -> SnapManager
func snapHeight(to view: View, multiplier: CGFloat = 1, isActive: Bool = true) -> SnapManager
func snap(size: CGSize, isActive: Bool = true) -> SnapManager
func snap(trailingView: View, constant: CGFloat = 0, isActive: Bool = true) -> SnapManager
func snap(leadingView: View, constant: CGFloat = 0, isActive: Bool = true) -> SnapManager
func snap(bottomView: View, constant: CGFloat = 0, isActive: Bool = true) -> SnapManager
func snap(topView: View, constant: CGFloat = 0, isActive: Bool = true) -> SnapManager
func snap(to view: View? = 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, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(to view: View? = nil, config: SnapConfig, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snapWidth(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snapHeight(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(size: CGSize, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(trailingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(leadingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(bottomView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
func snap(topView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager
```

### Sample Code
Expand All @@ -102,12 +102,26 @@ print(snapManager.top?.constant) # 8.0
print(snapManager.height?.constant) # 0.5
```
Snap calls may also be chained and will continue to return a `SnapManager`.
### Constants
### Config
A `SnapConfig ` struct is also available where a developer may list all of their constraint constants beforehand and provide this type to the snap method argument.

```swift
let config = SnapConfig(top: 50, leading: 50, trailing: 50, width: 30, centerX: 0)
button.snap(constants: config)
button.snap(config: config)
```

### Priority
SnapLayout assumes required priority (same default as Apple) unless otherwise specified for those created constraints.

```swift
button1.snap(trailingView: button2, constant: 8, priority: UILayoutPriorityDefaultLow)
```

In the following example, the top, leading, bottom, and trailing constraint all have a priority of required; however, the height constraint has a `UILayoutPriorityDefaultHigh` priority. That's it!

```swift
rectangleView.snap(top: 48, leading: 16, bottom: 16, trailing: 16)
.snap(height: 40, priority: UILayoutPriorityDefaultHigh)
```

### To Activate or not to Activate
Expand Down
2 changes: 1 addition & 1 deletion SnapLayout.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SnapLayout'
s.version = '1.7.0'
s.version = '1.8.0'
s.summary = 'Concise API for iOS Auto Layout'

s.description = <<-DESC
Expand Down
8 changes: 8 additions & 0 deletions SnapLayout/Classes/Aliases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
#if os(iOS) || os(tvOS)
/// Allows views to be OS independent while leveraging existing code
public typealias View = UIView
/// Allows layout priority to be OS independent while leveraging existing code
public typealias LayoutPriority = UILayoutPriority
/// Allows layout priority required to be OS independent while leveraging existing code
public var LayoutPriorityRequired = UILayoutPriorityRequired
#elseif os(OSX)
/// Allows views to be OS independent while leveraging existing code
public typealias View = NSView
/// Allows layout priority to be OS independent while leveraging existing code
public typealias LayoutPriority = NSLayoutPriority
/// Allows layout priority required to be OS independent while leveraging existing code
public var LayoutPriorityRequired = NSLayoutPriorityRequired
#endif

0 comments on commit 7b3ea6e

Please sign in to comment.