{"id":1296,"url":"https://github.com/sp71/SnapLayout","last_synced_at":"2025-07-30T20:33:06.035Z","repository":{"id":141561558,"uuid":"69823874","full_name":"sp71/SnapLayout","owner":"sp71","description":"Concise Auto Layout API to chain programmatic constraints while easily updating existing constraints.","archived":true,"fork":false,"pushed_at":"2019-09-14T19:35:13.000Z","size":395,"stargazers_count":11,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-14T14:06:30.224Z","etag":null,"topics":["autolayout","constraint-programming","constraints","ios","pin"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sp71.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-10-02T21:44:39.000Z","updated_at":"2023-01-28T17:14:46.000Z","dependencies_parsed_at":"2024-01-02T20:58:53.949Z","dependency_job_id":"2b748659-d0b6-4a67-b303-86b6825c2cad","html_url":"https://github.com/sp71/SnapLayout","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sp71%2FSnapLayout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sp71%2FSnapLayout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sp71%2FSnapLayout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sp71%2FSnapLayout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sp71","download_url":"https://codeload.github.com/sp71/SnapLayout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228187629,"owners_count":17882338,"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":["autolayout","constraint-programming","constraints","ios","pin"],"created_at":"2024-01-05T20:15:43.193Z","updated_at":"2024-12-04T20:31:16.179Z","avatar_url":"https://github.com/sp71.png","language":"Swift","readme":"# [![SnapLayout](https://github.com/sp71/SnapLayout/blob/master/Assets/snapLayout.png?raw=true)](#)\n[![Build Status](https://travis-ci.org/sp71/SnapLayout.svg?branch=master)](https://travis-ci.org/sp71/SnapLayout)\n[![CocoaPods](https://img.shields.io/cocoapods/metrics/doc-percent/SnapLayout.svg)](http://cocoadocs.org/docsets/SnapLayout)\n[![Coverage Status](https://coveralls.io/repos/github/sp71/SnapLayout/badge.svg)](https://coveralls.io/github/sp71/SnapLayout)\n[![Version](https://img.shields.io/cocoapods/v/SnapLayout.svg?style=flat)](http://cocoapods.org/pods/SnapLayout)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Platform](https://img.shields.io/cocoapods/p/SnapLayout.svg?style=flat)](http://cocoapods.org/pods/SnapLayout)\n[![License](https://img.shields.io/cocoapods/l/SnapLayout.svg?style=flat)](https://github.com/sp71/SnapLayout/blob/master/LICENSE)\n\nConcise API for Auto Layout. SnapLayout extends `UIView` and `NSView` to deliver a list of APIs to improve readability while also shortening constraint code. Internally uses AutoLayout to provide the best experience. With SnapLayout, developers can remove boilerplate code but not at the cost of readability.\n\nImagine applying any or all of the following constraints in one line of code to a view: top, leading, trailing, bottom, width, height, centerX, centerY. Not enough? Start chaining your snap calls to support adjacents snaps. This is all possible with `SnapLayout`.\n\n## Why Use It?\nLets use a real world example. Lets say the following view needs to be added. It has a centerX, centerY, width, and height constraint.\n \n![SnapLayout](https://github.com/sp71/SnapLayout/blob/master/Assets/exampleView.png?raw=true)\n\n[Apple](https://developer.apple.com/reference/uikit/nslayoutanchor):\n\n```swift\nsquareView.translatesAutoresizingMaskIntoConstraints = false\nsquareView.widthAnchor.constraint(equalToConstant: 50)\nsquareView.heightAnchor.constraint(equalToConstant: 50)\nsquareView.centerXAnchor.constraint(equalTo: squareSuperview.centerXAnchor, constant: 0)\nsquareView.centerYAnchor.constraint(equalTo: squareSuperview.centerYAnchor, constant: 0)\n```\n\nSnapLayout:\n\n```swift\nsquareView.snap(width: 50, height: 50, centerX: 0, centerY: 0)\n```\nSnapLayout handles `translatesAutoresizingMaskIntoConstraints` and references the superview of `squareView` when applying constraints. Built to be flexible, yet readable.\n\n## Setup\n### Requirements\n* Xcode\n  * Language Support: **Swift** *(5.0)*\n  * Fully Compatible With: **Xcode 10.0 and higher**\n* iOS\n  * Fully Compatible With: **iOS 9.0 and above**\n* macOS\n  * Fully Compatible With: **macOS 10.11 and above**\n* tvOS\n  * Fully Compatible With: **tvOS 9.0 and above**\n\n### Installation with [CocoaPods](http://cocoapods.org)\n\n```ruby\npod \"SnapLayout\"\n```\n\n### Installation with [Carthage](https://github.com/Carthage/Carthage)\n```\ngithub \"sp71/SnapLayout\"\n```\n\n## Usage\n\n### Overview \n\n* All methods are prefixed with `snap` for quick Xcode autocomplete.\n* Directly uses NSLayoutAnchor under the hood so the API is developer friendly\n* Any view using `SnapLayout` will not only have its `translatesAutoresizingMaskIntoConstraints` set to `false`, but also have its constraint activated.\n* Snap offers many default parameters including the `view` argument. If a view parameter is not supplied, it is assumed your view will be snapping to its superview.\n* Powerful API supporting snapping of adjacent views\n\n\n### [`Methods`](SnapLayout/Classes/SnapLayout.swift)\n```swift\nfunc 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) -\u003e SnapManager\nfunc snap(to view: View? = nil, config: SnapConfig, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snapWidth(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snapHeight(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snap(size: CGSize, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snap(trailingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snap(leadingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snap(bottomView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\nfunc snap(topView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -\u003e SnapManager\n```\n\n### Sample Code\n\n```swift\nlet buttonSnapManager = button.snap(top: 50, leading: 50, trailing: 50, width: 30)\nbuttonSnapManager.top?.constant = 100\n```\nNot only has this button applied 4 constraints to its superview, but each individual constraint is accessible through the returned `SnapManager` type. The beauty of SnapLayout is not only its powerful API interface, but how easy it is to adjust constraints. Other API's simply return an array, but not SnapLayout. Each constraint is neatly packaged into a `SnapManager`.\n\n### Snapping Adjacent Views\n```swift\nbutton1.snap(trailingView: button2, constant: 8)\n```\nThese buttons are now side by side where button2 is now the trailingView. No longer will developers have to think which trailing constraint should apply to which leading constraint. This keeps the code lean and clean.\n\n### Chaining\n```swift\nlet snapManager = view.snap(top: 8, leading: 8, width: 50)\n                      .snapHeight(to: superview, multiplier: 0.5)\nprint(snapManager.top?.constant)    # 8.0\nprint(snapManager.height?.constant) # 0.5\n```\nSnap calls may also be chained and will continue to return a `SnapManager`.\n### Config\nA `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.\n\n```swift\nlet config = SnapConfig(top: 50, leading: 50, trailing: 50, width: 30, centerX: 0)\nbutton.snap(config: config)\n```\n\n### Priority\nSnapLayout assumes required priority (same default as Apple) unless otherwise specified for those created constraints.\n\n```swift\nbutton1.snap(trailingView: button2, constant: 8, priority: .low)\n```\n\nIn the following example, the top, leading, bottom, and trailing constraint all have a priority of required; however, the height constraint has a `UILayoutPriority.defaultHigh` priority. That's it!\n\n```swift\nrectangleView.snap(top: 48, leading: 16, bottom: 16, trailing: 16)\n             .snap(height: 40, priority: .defaultHigh)\n```\n\n### To Activate or not to Activate\nSnapLayout, by default, activates all constraints it creates; however, this can be disabled by passing false to `isActive`.\n\n```swift\nbutton1.snap(trailingView: button2, constant: 8, isActive: false)\n```\n\n### Debugging\nSnapLayout will also print out errors to log if a `snap` was not properly applied.\n\n```\nSnapLayout Error - No constraint was applied for view: \u003cUIView: 0x7fcb8f4031d0; frame = (0 0; 0 0); layer = \u003cCALayer: 0x608000036320\u003e\u003e\n```\n\n### Safe Area Layout Guide Support\nSnapLayout will apply constraints using `safeAreaLayoutGuide` if the app is running iOS 11; otherwise, it will apply the view's layout anchors directly.\n\n### Example Apps\n\nTo run the example project, run `pod try SnapLayout`.\n\nThis example project contains not only an `Example-iOS` target, but also an `Example-MacOS` target. This is a great introduction to become more familiar with the library.\n\n## Blogs\n- [Medium](https://medium.com/@satindersingh71/snaplayout-5758417ee9a0)\n\n## Author\n\nSatinder Singh, satindersingh71@gmail.com\n\n## License\n\nSnapLayout is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":["Layout"],"sub_categories":["Other Hardware","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsp71%2FSnapLayout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsp71%2FSnapLayout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsp71%2FSnapLayout/lists"}