랭킹으로 돌아가기

roberthein/TinyConstraints

Swift

Nothing but sugar.

autolayoutswiftconstraintsnslayoutconstraintsnslayoutconstraintanimationsuperviewcentersugarstackswift4
스타 성장
스타
4.1k
포크
195
주간 성장
이슈
15
2k3k4k
2017년 2월2020년 3월2023년 5월2026년 7월
README

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.

TinyConstraints

Features

  • Pure Swift 5 sweetness.
  • Everything you can do with Auto Layout, but shorter.
  • Constraints are active by default.
  • 100% compatible with other Auto Layout code.
  • Optionally store your constraints.
  • Set constraint priorities upon creation.
  • Constrain directly to the superview.
  • Stack views together with one line of code.
  • No need to set translatesAutoresizingMaskIntoConstraints because TinyConstraints does it for you.

Examples

Edges

Attaching a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0),
    view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0),
    view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0),
    view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0)
])

with TinyConstraints:

view.edgesToSuperview()

or:

view.edgesToSuperview(insets: .top(10) + .left(10))

Center

Constraining the center of a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.centerXAnchor.constraint(equalTo: superview.centerXAnchor, constant: 0)
    view.centerYAnchor.constraint(equalTo: superview.centerYAnchor, constant: 0)
])

with TinyConstraints:

view.center(in: superview)

or:

view.center(in: superview, offset: CGPoint(x: 10, y: 10))

Basic Use

Typealiases

TinyConstraints gives you convenient and tiny typealiases for handling constraints.

  • Constraint = NSLayoutConstraint
  • Constraints = [NSLayoutConstraint]

Equal and Unequal Anchors

This constraints the top-anchor of the view to the top-anchor of the superview:

view.top(to: superview)

This constraints the top-anchor of firstView to the bottom-anchor of secondView:

firstView.topToBottom(of: secondView)

Constrain to Superview

Often you need to constrain a view to it's superview, with TinyConstraints you can do this super easy:

view.edgesToSuperview()

Or only one edge:

view.topToSuperview()

Or you can attach all edges except one, like this:

view.edgesToSuperview(excluding: .bottom)

Relation and Priority

For almost all constraints you can set the relation and priority properties. The default relation is .equal and the default priority is .required:

container.width(150, relation: .equalOrLess, priority: .high)

Storing Constraints

Here we create a set of inactive constraints and store these to our property:

let constraints = view.size(CGSize(width: 100, height: 100), isActive: false)

Activation and Deactivation

Besides the default NSLayoutConstraint activation, TinyConstraints also provides a way to activate a set of constraints:

constraints.activate()

You can also do this in an animation:

oldConstraints.deActivate()

constraints.activate()
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Animating Constraint Constants

Here we add a height constraint to a view, store it and animate it later:

let height = view.height(100)

height.constant = 200
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Stack

Stack provides a way of constraining views together in a superview:

let views = [logo, title, description]
superview.stack(views, axis: .vertical, spacing: 10)
Find these examples and more in the Example Project.

Installation

CocoaPods

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

pod "TinyConstraints"

Carthage

TinyConstraints is available through Carthage. To install it, simply add the following line to your Cartfile:

github "roberthein/TinyConstraints"

Swift Package Manager

TinyConstraints is available through Swift Package Manager. To install it, in Xcode 11.0 or later select File > Swift Packages > Add Package Dependency... and add TinyConstraints repository URL:

https://github.com/roberthein/TinyConstraints.git

Tutorials

Here are some video tutorials made by Alex Nagy.

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

관련 저장소
gkd-kit/gkd

基于无障碍,高级选择器,订阅规则的自定义屏幕点击安卓应用 | An Android APP with custom screen tapping based on Accessibility, Advanced Selectors, and Subscription Rules

KotlinGNU General Public License v3.0androidkotlin
gkd.li
40.3k1.9k
SnapKit/SnapKit

A Swift Autolayout DSL for iOS & OS X

SwiftMIT Licenseautoautolayout
snapkit.github.io/SnapKit/
20.3k2k
zhouxiaoka/autoclip

AutoClip : AI-powered video clipping and highlight generation · 一款智能高光提取与剪辑的二创工具

PythonPyPIMIT Licenseaiai-agents
zhouxiaoka.github.io/autoclip_intro/
6.2k1.2k
GhostTroops/scan4all

Official repository vuls Scan: 15000+PoCs; 23 kinds of application password crack; 7000+Web fingerprints; 146 protocols and 90000+ rules Port scanning; Fuzz, HW, awesome BugBounty( ͡° ͜ʖ ͡°)...

GoGo ModulesBSD 3-Clause "New" or "Revised" Licenseattackauto
scan4all.51pwn.com
6.1k719
zfdang/Android-Touch-Helper

AdSkip — an Android assistant for automatically skipping app launch ads

JavaMavenMIT Licenseandroidsplash
touchhelper.zfdang.com
5.3k504
certd/certd

开源SSL证书管理工具;全自动证书申请、更新、续期;通配符证书,泛域名证书申请;证书自动化部署到阿里云、腾讯云、主机、群晖、宝塔;https证书,pfx证书,der证书,TLS证书,nginx证书自动续签自动部署

JavaScriptnpmGNU Affero General Public License v3.0certbotletencrypt
certd.docmirror.cn
4.9k572
GuoXiCheng/SKIP

自动跳过APP开屏广告

KotlinGNU General Public License v3.0androidauto
skip.guoxicheng.top
3.6k253
freshOS/Stevia

:leaves: Concise Autolayout code

SwiftMIT Licenseautolayoutswift
freshos.github.io/SteviaDocs/
3.4k220
dundunnp/auto_xuexiqiangguo

学习强国 每日拿满61分!免root 四人赛双人对战秒答 自动化脚本

JavaScriptnpmMIT Licensexuexiqiangguohamibot
dundunnp.github.io/auto_xuexiqiangguo/
2.5k875
nakiostudio/EasyPeasy

Auto Layout made easy

SwiftMIT Licenseconstraintsauto
easy-peasy.io
1.9k116
kimwang1978/collect-txt

源 (自动整理)

HTMLtvboxlive
iptv365.org
1.9k431
kimwang1978/collect-tv-txt

TV直播源 (每日自动收集・持续更新)

PythonPyPItvboxlive
live.iptv365.org/live.txt
1.4k328