{"id":1281,"url":"https://github.com/puffinsupply/Restraint","last_synced_at":"2025-07-30T20:33:02.516Z","repository":{"id":31262549,"uuid":"34824246","full_name":"puffinsupply/Restraint","owner":"puffinsupply","description":"Minimal Auto Layout in Swift","archived":false,"fork":false,"pushed_at":"2020-11-26T06:51:13.000Z","size":115,"stargazers_count":79,"open_issues_count":0,"forks_count":11,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-14T14:06:29.329Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/puffinsupply.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-29T23:46:00.000Z","updated_at":"2024-06-25T04:41:46.000Z","dependencies_parsed_at":"2022-09-09T07:20:32.275Z","dependency_job_id":null,"html_url":"https://github.com/puffinsupply/Restraint","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puffinsupply%2FRestraint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puffinsupply%2FRestraint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puffinsupply%2FRestraint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puffinsupply%2FRestraint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/puffinsupply","download_url":"https://codeload.github.com/puffinsupply/Restraint/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228187624,"owners_count":17882335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:42.853Z","updated_at":"2024-12-04T20:31:13.732Z","avatar_url":"https://github.com/puffinsupply.png","language":"Swift","funding_links":[],"categories":["Layout","Libs"],"sub_categories":["Other Hardware","Other free courses","Layout"],"readme":"# Restraint\n\nRestraint is a very very small library to help make your use of `NSLayoutConstraint` in Swift more legible \u0026 declarative.\n\n- Like programmatic views?\n- Like the benefits of using pure AutoLayout?\n- Like clear and minimal interfaces?\n- Dislike Visual Format Language and \"stringly\" typing?\n- Dislike the verbosity of `NSLayoutConstraint`?\n- Dislike heavy dependencies?\n- Practice `Restraint`!\n\n![Carthage](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)\n\n## Features\n\n- [x] As simple as possible\n- [x] Easy to maintain\n- [x] Easy to replace\n- [x] Easy to circumvent\n- [x] Not too clever\n- [x] Sane defaults\n- [x] Automatic handling of `setTranslatesAutoresizingMaskIntoConstraints`\n\n## Basic Example\n\nLet's set the height \u0026 width of an `imageView` to `200` points and center in the current `UIView`. Here's how we would do that with `NSLayoutConstraint`:\n\n```swift\nlet imageViewWidthConstraint = NSLayoutConstraint(\n  item:       imageView,\n  attribute:  .Height,\n  relatedBy:  .Equal,\n  toItem:     nil,\n  attribute:  .NotAnAttribute,\n  multiplier: 1.0,\n  constant:   200\n)\n\nlet imageViewHeightConstraint = NSLayoutConstraint(\n  item:       imageView,\n  attribute:  .Width,\n  relatedBy:  .Equal,\n  toItem:     nil,\n  attribute:  .NotAnAttribute,\n  multiplier: 1.0,\n  constant:   200\n)\n\nimageView.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint])\n\nlet imageViewHorizontalConstraint = NSLayoutConstraint(\n  item:       imageView,\n  attribute:  .CenterX,\n  relatedBy:  .Equal,\n  toItem:     self,\n  attribute:  .CenterX,\n  multiplier: 1.0,\n  constant:   0\n)\n\nlet imageViewVerticalConstraint = NSLayoutConstraint(\n  item:       imageView,\n  attribute:  .CenterY,\n  relatedBy:  .Equal,\n  toItem:     self,\n  attribute:  .CenterY,\n  multiplier: 1.0,\n  constant:   0\n)\n\naddConstraints([imageViewHorizontalConstraint, imageViewVerticalConstraint])\n```\n\n### :fearful:\n\nAnd of course, this depends upon your particular formatting conventions. You know how quickly this gets out of hand.\n\nHere's how to apply these same constraints with `Restraint`:\n\n```swift\nRestraint(imageView, .Width,  .Equal, 200).addToView(imageView)\nRestraint(imageView, .Height, .Equal, 200).addToView(imageView)\n\nRestraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)\nRestraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)\n```\n\n### :massage:\n\n## Detailed Example\n\n![Example View](Example.png)\n\nFor this view, we'd like to:\n\n- Center the `imageView` (which is of a fixed size) vertically and horizontally in the containing view.\n- Center `topLabel` (which is of a variable size) vertically between the top of `imageView` and the top of the containing view.\n- Center `topLabel` horizontally in the containing view.\n- Center `bottomLabel` (which is of a variable size) vertically between the bottom of `imageView` and the bottom of the containing view.\n- Set the width of `bottomLabel` to the width of the containing view less its layout margins.\n\nHere are those rules expressed with `Restraint`:\n\n```swift\n// Image View Constraints\n\nRestraint(imageView, .Width,  .Equal, imageViewSize).addToView(self)\nRestraint(imageView, .Height, .Equal, imageViewSize).addToView(self)\n\nRestraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)\nRestraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)\n\n// Top Label Constraints\n\nRestraint(topLabel, .CenterX, .Equal, imageView, .CenterX).addToView(self)\nRestraint(topLabel, .CenterY, .Equal, imageView, .Top, 0.5, 0).addToView(self)\n\n// Bottom Label Constraints\n\nRestraint(bottomLabel, .Left,  .Equal, self, .LeftMargin).addToView(self)\nRestraint(bottomLabel, .Right, .Equal, self, .RightMargin).addToView(self)\n\nRestraint(bottomLabel, .CenterY, .Equal, self, .CenterY, 1.5, (200 / 4)).addToView(self)\n```\n\nYou can see this in action in [Example/View.swift](Example/Example/View.swift)\n\n\n## How It Works\n\nEach `Restraint` simply creates the appropriate `NSLayoutConstraint`, and the call to `addToView` disables `translatesAutoresizingMaskIntoConstraints` on the left view in the constraint and adds that constraint to the target view.\n\nOptionally, you can call `Restraint().constraint()` to get an instance of `NSLayoutConstraint` and add your constraint with `UIView#addConstraint` or `#addConstraints` later. For example:\n\n```swift\nlet heightConstraint = Restraint(imageView, .Height, .Equal, 200).constraint()\n\nimageView.addConstraint(heightConstraint)\n```\n\nIn this case you'll need to handle disabling `translatesAutoresizingMaskIntoConstraints` yourself.\n\n## Installation\n\n### Carthage\n\nAdd the following to your project's `Cartfile`:\n\n```swift\ngithub \"puffinsupply/Restraint\" \u003e= 1.0\n```\n\n### Manual\n\nSimply add [Restraint.swift](Restraint/Restraint.swift) to your project.\n\n\n## Contributors\n\n- [Adam Michela](https://github.com/soopa/)\n- [Michael Bachand](https://github.com/bachand/)\n- [Drew Yeaton](https://github.com/drewyeaton/)\n\n\n## License\n\n- [MIT](http://thi.mit-license.org/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpuffinsupply%2FRestraint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpuffinsupply%2FRestraint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpuffinsupply%2FRestraint/lists"}