{"id":1287,"url":"https://github.com/Rightpoint/Anchorage","last_synced_at":"2025-07-30T20:33:02.904Z","repository":{"id":7436926,"uuid":"56383822","full_name":"Rightpoint/Anchorage","owner":"Rightpoint","description":"A collection of operators and utilities that simplify iOS layout code.","archived":false,"fork":false,"pushed_at":"2023-10-27T15:05:47.000Z","size":360,"stargazers_count":632,"open_issues_count":20,"forks_count":47,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-12-01T15:11:39.997Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rightpoint.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-04-16T13:02:45.000Z","updated_at":"2024-10-03T18:53:02.000Z","dependencies_parsed_at":"2022-08-20T18:50:40.411Z","dependency_job_id":"bf9a7892-e48c-4144-86be-5c83e4aa3653","html_url":"https://github.com/Rightpoint/Anchorage","commit_stats":{"total_commits":221,"total_committers":26,"mean_commits":8.5,"dds":0.6470588235294117,"last_synced_commit":"d5655ec0c1f884d50217e3435b2b152748ca0328"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rightpoint%2FAnchorage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rightpoint%2FAnchorage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rightpoint%2FAnchorage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rightpoint%2FAnchorage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rightpoint","download_url":"https://codeload.github.com/Rightpoint/Anchorage/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:43.003Z","updated_at":"2024-12-04T20:31:14.156Z","avatar_url":"https://github.com/Rightpoint.png","language":"Swift","funding_links":[],"categories":["Layout"],"sub_categories":["Other Hardware"],"readme":"# Anchorage\n\n[![Swift 4.2 + 5.0](https://img.shields.io/badge/Swift-4.2%20+%205.0-orange.svg?style=flat)](https://swift.org)\n[![CircleCI](https://img.shields.io/circleci/project/github/Rightpoint/Anchorage/master.svg)](https://circleci.com/gh/Rightpoint/Anchorage)\n[![Version](https://img.shields.io/cocoapods/v/Anchorage.svg?style=flat)](https://cocoadocs.org/docsets/Anchorage)\n[![Platform](https://img.shields.io/cocoapods/p/Anchorage.svg?style=flat)](http://cocoapods.org/pods/Anchorage)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nA lightweight collection of intuitive operators and utilities that simplify Auto Layout code. Anchorage is built directly on top of the `NSLayoutAnchor` API.\n\nEach expression acts on one or more `NSLayoutAnchor`s, and returns active `NSLayoutConstraint`s. If you want inactive constraints, [here's how to do that](#batching-constraints).\n\n# Usage\n\n## Alignment\n\n```swift\n// Pin the button to 12 pt from the leading edge of its container\nbutton.leadingAnchor == container.leadingAnchor + 12\n\n// Pin the button to at least 12 pt from the trailing edge of its container\nbutton.trailingAnchor \u003c= container.trailingAnchor - 12\n\n// Center one or both axes of a view\nbutton.centerXAnchor == container.centerXAnchor\nbutton.centerAnchors == container.centerAnchors\n```\n\n## Relative Alignment\n\n```swift\n// Position a view to be centered at 2/3 of its container's width\nview.centerXAnchor == 2 * container.trailingAnchor / 3\n\n// Pin the top of a view at 25% of container's height\nview.topAnchor == container.bottomAnchor / 4\n```\n\n## Sizing\n\n```swift\n// Constrain a view's width to be at most 100 pt\nview.widthAnchor \u003c= 100\n\n// Constraint a view to a fixed size\nimageView.sizeAnchors == CGSize(width: 100, height: 200)\n\n// Constrain two views to be the same size\nimageView.sizeAnchors == view.sizeAnchors\n\n// Constrain view to 4:3 aspect ratio\nview.widthAnchor == 4 * view.heightAnchor / 3\n```\n\n## Composite Anchors\n\nConstrain multiple edges at a time with this syntax:\n\n```swift\n// Constrain the leading, trailing, top and bottom edges to be equal\nimageView.edgeAnchors == container.edgeAnchors\n\n// Inset the edges of a view from another view\nlet insets = UIEdgeInsets(top: 5, left: 10, bottom: 15, right: 20)\nimageView.edgeAnchors == container.edgeAnchors + insets\n\n// Inset the leading and trailing anchors by 10\nimageView.horizontalAnchors \u003e= container.horizontalAnchors + 10\n\n// Inset the top and bottom anchors by 10\nimageView.verticalAnchors \u003e= container.verticalAnchors + 10\n```\n\n#### Use leading and trailing\nUsing `leftAnchor` and `rightAnchor` is rarely the right choice. To encourage this, `horizontalAnchors` and `edgeAnchors` use the `leadingAnchor` and `trailingAnchor` layout anchors.\n\n#### Inset instead of Shift\nWhen constraining leading/trailing or top/bottom, it is far more common to work in terms of an inset from the edges instead of shifting both edges in the same direction. When building the expression, Anchorage will flip the relationship and invert the constant in the constraint on the far side of the axis. This makes the expressions much more natural to work with.\n\n\n## Priority\n\nThe `~` is used to specify priority of the constraint resulting from any Anchorage expression:\n\n```swift\n// Align view 20 points from the center of its superview, with system-defined low priority\nview.centerXAnchor == view.superview.centerXAnchor + 20 ~ .low\n\n// Align view 20 points from the center of its superview, with (required - 1) priority\nview.centerXAnchor == view.superview.centerXAnchor + 20 ~ .required - 1\n\n// Align view 20 points from the center of its superview, with custom priority\nview.centerXAnchor == view.superview.centerXAnchor + 20 ~ 752\n```\nThe layout priority is an enum with the following values:\n\n- `.required` - `UILayoutPriorityRequired` (default)\n- `.high` - `UILayoutPriorityDefaultHigh`\n- `.low` - `UILayoutPriorityDefaultLow`\n- `.fittingSize` - `UILayoutPriorityFittingSizeLevel`\n\n## Storing Constraints\n\nTo store constraints created by Anchorage, simply assign the expression to a variable:\n\n```swift\n// A single (active) NSLayoutConstraint\nlet topConstraint = (imageView.topAnchor == container.topAnchor)\n\n// EdgeConstraints represents a collection of constraints\n// You can retrieve the NSLayoutConstraints individually,\n// or get an [NSLayoutConstraint] via .all, .horizontal, or .vertical\nlet edgeConstraints = (button.edgeAnchors == container.edgeAnchors).all\n```\n\n## Batching Constraints\n\nBy default, Anchorage returns active layout constraints. If you'd rather return inactive constraints for use with the [`NSLayoutConstraint.activate(_:)` method](https://developer.apple.com/reference/uikit/nslayoutconstraint/1526955-activate) for performance reasons, you can do it like this:\n\n```swift\nlet constraints = Anchorage.batch(active: false) {\n    view1.widthAnchor == view2.widthAnchor\n    view1.heightAnchor == view2.heightAnchor / 2 ~ .low\n    // ... as many constraints as you want\n}\n\n// Later:\nNSLayoutConstraint.activate(constraints)\n```\n\nYou can also pass `active: true` if you want the constraints in the array to be automatically activated in a batch.\n\n## Autoresizing Mask\n\nAnchorage sets the `translatesAutoresizingMaskIntoConstraints` property to `false` on the *left* hand side of the expression, so you should never need to set this property manually. This is important to be aware of in case the container view relies on `translatesAutoresizingMaskIntoConstraints` being set to `true`. We tend to keep child views on the left hand side of the expression to avoid this problem, especially when constraining to a system-supplied view.\n\n## A Note on Compile Times\n\nAnchorage overloads a few Swift operators, which can lead to increased compile times. You can reduce this overhead by surrounding these operators with `/`, like so:\n\n| Operator | Faster Alternative |\n|------|----------|\n| `==` | `/==/` |\n| `\u003c=` | `/\u003c=/` |\n| `\u003e=` | `/\u003e=/` |\n\nFor example, `view1.edgeAnchors == view2.edgeAnchors` would become `view1.edgeAnchors /==/ view2.edgeAnchors`.\n\n# Installation\n\n## CocoaPods\n\nTo integrate Anchorage into your Xcode project using CocoaPods, specify it in\nyour Podfile:\n\n```ruby\npod 'Anchorage'\n```\n\n## Carthage\n\nTo integrate Anchorage into your Xcode project using Carthage, specify it in\nyour Cartfile:\n\n```ogdl\ngithub \"Rightpoint/Anchorage\"\n```\n\nRun `carthage update` to build the framework and drag the built\n`Anchorage.framework` into your Xcode project.\n\n# License\n\nThis code and tool is under the MIT License. See `LICENSE` file in this repository.\n\nAny ideas and contributions welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRightpoint%2FAnchorage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRightpoint%2FAnchorage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRightpoint%2FAnchorage/lists"}