Latest 1.3.0
Homepage https://github.com/DevAndArtist/swift-functionallayout
License MIT
Platforms ios 10.0
Frameworks UIKit
Authors

FunctionalLayoutFunctionalLayoutFunctionalLayout
FunctionalLayout


FunctionalLayout



FunctionalLayout

FunctionalLayout

Write beautiful constraints:

import FunctionalLayout

let constraint = Constraint.where(view1.centerXAnchor == view2.centerXAnchor)
    .named("CenterXConstraint")
    .withPriority(of: 800)
    .shouldBeArchived()
    .nsLayoutConstraint

constraint.isActive = true

self.constraint = Constraint.where(view1.centerYAnchor == view2.centerYAnchor)
    .named("CenterYConstraint")
    .activated()
    .nsLayoutConstraint

NSLayoutConstraint.activate([
    Constraint.where(view1.widthAnchor == 200).named("WidthConstraint"),
    Constraint.where(view1.heightAnchor == view2.heightAnchor)
        .multiplied(with: 2.0)
        .offset(by: -50)
        .named("HeightConstraint")
        .withPriority(of: 1000)
        .shouldBeArchived()
        .assignedTo(&self.someConstraintVariable)
])

Installation

FunctionalLayout is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'FunctionalLayout'

Explanation

This API contains a complex function permutation graph modeled with protocols. Each letter represents a function that the coresponding protocol contains.

Function permutation protocol graph

Red nodes represent the starting points for the four static functions:

Constraint.where(_: Layout<NSLayoutXAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutYAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutDimension>) -> OMWSN
Constraint.where(_: Layout<CGFloat>) -> WSN

An instance of Layout<...> can be constructed with three different infix operators.

func ==<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func >=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func <=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>

func ==(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func >=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func <=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>

This API guarantees value semantics [1] for all types except for ActivationInterface and NSLayoutConstraintInterface. For all guaranteed value semantics types, each chain creates a new immutable value type instance under the hood.

[1] Value semantics: realm.io/news/swift-gallagher-value-semantics

Todo

  • [x] Create clean API.
  • [ ] Test Carthage compatibility.
  • [ ] Link to CodeCov and Travis CI.
  • [ ] Write docs.
  • [ ] Write tests.

License

FunctionalLayout is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "FunctionalLayout",
    "version": "1.3.0",
    "summary": "A wrapper around `NSLayoutAnchor` API.",
    "description": "This CocoaPod provides the ability to construct instances of the `NSLayoutConstraint` in a beautiful functional approach by wrapping around the `NSLayoutAnchor` API.",
    "homepage": "https://github.com/DevAndArtist/swift-functionallayout",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Adrian Zubarev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/DevAndArtist/swift-functionallayout.git",
        "tag": "1.3.0"
    },
    "social_media_url": "https://twitter.com/DevAndArtist",
    "platforms": {
        "ios": "10.0"
    },
    "source_files": "Sources/**/*.swift",
    "frameworks": "UIKit",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This