Swiftpack.co - sugarmo/AnchorPal as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
sugarmo/AnchorPal
An AutoLayout library inspired by SnapKit.
.package(url: "https://github.com/sugarmo/AnchorPal.git", from: "0.9.7")

AnchorPal

An AutoLayout library inspired by SnapKit.

Installation

CocodPods

Add this line to your Podfile

pod 'AnchorPal'

Manually

Add AnchorPal.xcodeproj to your current project.

Usage

If you are familiar with SnapKit, you already know how to use AnchorPal, just with a little changes.

Install new constraints.

view.anc.installConstraints { d in
    d.xEdges.equalToSuperview()
    // with a optional parameter, you can easily refer to the layoutGuides of superview
    d.yEdges.equalToSuperview(\.safeArea)
}

Reinstall constraints.

view.anc.reinstallConstraints { d in
    d.height.equalTo(newValue)
}

Make constraints without installing.

let constaints = view.anc.makeConstraints { d in
    d.edges.equalToSuperview(\.safeArea)
}

// Activate them later
constraints.activate()

Inset

Note: We don't use first.relationTo(second).inset(x) format, because it's ambiguous.

When we say first.>=(second).inset(x), it can be "The first item has a greater value than second item insetting x.", but also can be "The inset from second item to first item is greater than x.". We want to be clear about the intent.

view.anc.installConstraints { d in
    d.edges.insetFromSuperview().equalTo(30)
}
// or 
view.anc.installConstraints { d in
    d.edges.insetFrom(otherView).greaterEqualTo(30)
}

Set dynamic constraint constant.

view.anc.installConstraints { d in
    d.width.equalTo { position -> CGFloat in
        // return a CGFloat as the new constant
        // position is the info about the current anchor which is calling this closure
        return newWidth
    }
}

// update constants later
view.anc.updateConstraintConstants()

Custom Dimension (iOS 10+, tvOS 10+, macOS 10.12+)

view1.anc.reinstallConstraints { d in
    let space1 = d.trailing.spaceBefore(view2.anc.leading)
    let space2 = d.bottom.spaceBefore(view3.anc.top)
    space1.equalTo(space2)
}

System Spacing (iOS 11+, tvOS 11+, macOS 11+)

// custom dimension to system spcaing
view1.anc.reinstallConstraints { d in
    d.leading.spaceAfter(view2.anc.trailing).equalToSystemSpacing().multiply(2)
}

// or anchor to anchor 
view1.anc.reinstallConstraints { d in
    d.leading.equalToSystemSpacingAfter(view2.anc.trailing).multiply(2)
}

Install or make constraints within static function.

// Sometimes you just don't want to bind these constraints to any view.
Anc.installConstraints {
    view1.anc.edges.equalToSuperview(\.safeArea)
    view2.anc.top.equalTo(view1.anc.bottom).plus(20)
}

Install or make constraints outside the closure.

// make and install
view1.anc.edges.equalToSuperview(\.safeArea).active()
view2.anc.top.equalTo(view1.anc.bottom).plus(20).active()

// or just make
let constraint1 = view1.anc.edges.equalToSuperview(\.safeArea)
let constraint2 = view2.anc.top.equalTo(view1.anc.bottom).plus(20)

About the relation name.

We change the lessThanOrEqualTo to lessEqualTo, and greaterThanOrEqualTo to greaterEqualTo, just be shorter, but still has no ambiguity.

Closing

AnchorPal is designed to be flexbile and readable, hope you can enjoy it, if don't, please feel free to tell us.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics