Repository: kean/Align Branch: main Commit: 8c129b833af9 Files: 43 Total size: 145.7 KB Directory structure: gitextract_bpzxhb01/ ├── .github/ │ ├── FUNDING.yml │ └── workflows/ │ └── ci.yml ├── .gitignore ├── .scripts/ │ └── build-docs.sh ├── Align.xcodeproj/ │ ├── project.pbxproj │ ├── project.xcworkspace/ │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata/ │ │ └── IDEWorkspaceChecks.plist │ └── xcshareddata/ │ └── xcschemes/ │ └── Align.xcscheme ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── Package.swift ├── README.md ├── Sources/ │ ├── Align+Preview.swift │ ├── Align.docc/ │ │ ├── Align.md │ │ ├── Alignment+Extension.md │ │ ├── Anchor+Extensions.md │ │ ├── AnchorCollectionCenter+Extension.md │ │ ├── AnchorCollectionEdges+Extension.md │ │ ├── AnchorCollectionSize+Extension.md │ │ ├── Constraints+Extension.md │ │ └── LayoutAnchors+Extension.md │ ├── Anchor.swift │ ├── AnchorCollectionCenter.swift │ ├── AnchorCollectionEdges.swift │ ├── AnchorCollectionSize.swift │ ├── Constraints.swift │ ├── LayoutAnchors.swift │ └── LayoutItem.swift └── Tests/ ├── AnchorAPIsTests.swift ├── AnchorAlignmentTests.swift ├── AnchorCenterTests.swift ├── AnchorCollectionCenterTests.swift ├── AnchorCollectionEdgesTests.swift ├── AnchorCollectionSizeTests.swift ├── AnchorDimensionTests.swift ├── AnchorEdgeTests.swift ├── AnchorPerformanceTests.swift ├── AnchorTests.swift ├── ConstraintsTests.swift └── Extensions/ ├── Align+Extensions.swift ├── Diff.swift └── XCTestExtensions.swift ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: kean patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] ================================================ FILE: .github/workflows/ci.yml ================================================ name: "Align CI" on: push: branches: - main pull_request: branches: - '*' concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: ios-latest: name: Unit Tests (iOS 26.2, Xcode 26.2) runs-on: macOS-26 timeout-minutes: 20 env: DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer steps: - uses: actions/checkout@v4 - name: Boot simulator run: | xcrun simctl boot "iPhone 17 Pro" xcrun simctl bootstatus "iPhone 17 Pro" -b - name: Run Tests run: xcodebuild test -scheme "Align" -destination "OS=26.2,name=iPhone 17 Pro" | xcbeautify macos-latest: name: Unit Tests (macOS, Xcode 26.2) runs-on: macOS-26 timeout-minutes: 20 env: DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer steps: - uses: actions/checkout@v4 - name: Run Tests run: xcodebuild test -scheme "Align" -destination "platform=macOS" | xcbeautify tvos-latest: name: Unit Tests (tvOS 26.2, Xcode 26.2) runs-on: macOS-26 timeout-minutes: 20 env: DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer steps: - uses: actions/checkout@v4 - name: Boot simulator run: | xcrun simctl boot "Apple TV" xcrun simctl bootstatus "Apple TV" -b - name: Run Tests run: xcodebuild test -scheme "Align" -destination "OS=26.2,name=Apple TV" | xcbeautify macos-xcode-26-0: name: Unit Tests (macOS, Xcode 26.0) runs-on: macOS-26 timeout-minutes: 20 env: DEVELOPER_DIR: /Applications/Xcode_26.0.app/Contents/Developer steps: - uses: actions/checkout@v4 - name: Run Tests run: xcodebuild test -scheme "Align" -destination "platform=macOS" | xcbeautify swift-build: name: Swift Build (SPM) runs-on: macOS-26 timeout-minutes: 20 env: DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer steps: - uses: actions/checkout@v4 - name: Build run: swift build ================================================ FILE: .gitignore ================================================ # Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore ## Build generated build/ DerivedData/ .swiftpm/ ## Various settings *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata/ ## Other *.moved-aside *.xccheckout *.xcscmblueprint ## Obj-C/Swift specific *.hmap *.ipa *.dSYM.zip *.dSYM ## Playgrounds timeline.xctimeline playground.xcworkspace # Swift Package Manager # # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. # Packages/ # Package.pins .build/ # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control # # Pods/ # Carthage # # Add this line if you want to avoid checking in source code from Carthage dependencies. # Carthage/Checkouts Carthage/Build # fastlane # # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the # screenshots whenever they are needed. # For more information about the recommended setup visit: # https://docs.fastlane.tools/best-practices/source-control/#source-control fastlane/report.xml fastlane/Preview.html fastlane/screenshots fastlane/test_output ================================================ FILE: .scripts/build-docs.sh ================================================ #!/usr/bin/env bash # # Generates a static, self-hostable DocC site for the Align package. # # Output layout (default OUTPUT_DIR=.build/docs): # .build/docs/index.html # .build/docs/documentation/... # .build/docs/data/, css/, js/, images/, ... # # Serve the directory from any static host. For GitHub Pages under a # project repo, the site is reachable at: # https://.github.io//documentation/align/ # # Environment overrides: # SCHEME Xcode scheme to build (default: Align) # DESTINATION xcodebuild destination (default: generic/platform=iOS) # HOSTING_BASE_PATH URL path prefix for hosting (default: Align) # Use "" when serving from the domain root. # OUTPUT_DIR Final docs directory (default: .build/docs) # DERIVED_DATA Xcode derived data path (default: .build/docc-derived-data) set -euo pipefail SCHEME="${SCHEME:-Align}" DESTINATION="${DESTINATION:-generic/platform=iOS}" HOSTING_BASE_PATH="${HOSTING_BASE_PATH-Align}" OUTPUT_DIR="${OUTPUT_DIR:-.build/docs}" DERIVED_DATA="${DERIVED_DATA:-.build/docc-derived-data}" if [ -n "$HOSTING_BASE_PATH" ]; then DOCC_FLAGS="--transform-for-static-hosting --hosting-base-path $HOSTING_BASE_PATH" else DOCC_FLAGS="--transform-for-static-hosting" fi echo "==> Building DocC archive (scheme: $SCHEME, destination: $DESTINATION)" xcodebuild docbuild \ -scheme "$SCHEME" \ -destination "$DESTINATION" \ -derivedDataPath "$DERIVED_DATA" \ OTHER_DOCC_FLAGS="$DOCC_FLAGS" ARCHIVE=$(find "$DERIVED_DATA" -type d -name "${SCHEME}.doccarchive" | head -n 1) if [ -z "$ARCHIVE" ]; then echo "Error: could not locate ${SCHEME}.doccarchive under $DERIVED_DATA" >&2 exit 1 fi echo "==> Extracting static site from $ARCHIVE" rm -rf "$OUTPUT_DIR" mkdir -p "$OUTPUT_DIR" cp -R "$ARCHIVE/" "$OUTPUT_DIR/" echo "==> Done. Self-hostable docs at: $OUTPUT_DIR" if [ -n "$HOSTING_BASE_PATH" ]; then echo " Entry point: /$HOSTING_BASE_PATH/documentation/align/" else echo " Entry point: /documentation/align/" fi ================================================ FILE: Align.xcodeproj/project.pbxproj ================================================ // !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 54; objects = { /* Begin PBXBuildFile section */ 0C683A111FBA462D002ACEB9 /* AnchorCollectionEdgesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C683A0F1FBA4624002ACEB9 /* AnchorCollectionEdgesTests.swift */; }; 0C81ABB71C889C100036DFD4 /* Align.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C81ABB61C889C100036DFD4 /* Align.swift */; }; 0C8FD7791FB85E8B00A20E3D /* XCTestExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7781FB85E8B00A20E3D /* XCTestExtensions.swift */; }; 0C8FD77F1FB85EA500A20E3D /* Diff.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD77E1FB85EA500A20E3D /* Diff.swift */; }; 0C8FD7821FB85F5600A20E3D /* AnchorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7811FB85F5600A20E3D /* AnchorTests.swift */; }; 0C8FD7851FB8608E00A20E3D /* ConstraintsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7841FB8608E00A20E3D /* ConstraintsTests.swift */; }; 0C8FD78E1FB88B9B00A20E3D /* AnchorAlignmentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD78D1FB88B9B00A20E3D /* AnchorAlignmentTests.swift */; }; 0C8FD7911FB88BC300A20E3D /* AnchorEdgeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7901FB88BC300A20E3D /* AnchorEdgeTests.swift */; }; 0C8FD7941FB88BED00A20E3D /* AnchorCenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7931FB88BED00A20E3D /* AnchorCenterTests.swift */; }; 0C8FD7971FB88C0600A20E3D /* AnchorDimensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7961FB88C0600A20E3D /* AnchorDimensionTests.swift */; }; 0C8FD79A1FB88CE100A20E3D /* AnchorCollectionCenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD7991FB88CE100A20E3D /* AnchorCollectionCenterTests.swift */; }; 0C8FD79D1FB88D2100A20E3D /* AnchorCollectionSizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C8FD79C1FB88D2100A20E3D /* AnchorCollectionSizeTests.swift */; }; 0CBC06B8249E9F790055D1B1 /* AnchorPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBC06B7249E9F790055D1B1 /* AnchorPerformanceTests.swift */; }; 0CC3A06C1D7476A700754E59 /* Align.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0CE361181C86077B00EA70CF /* Align.framework */; }; 0CCC2073287E3D26001FC3CE /* Align.docc in Sources */ = {isa = PBXBuildFile; fileRef = 0CCC2072287E3D26001FC3CE /* Align.docc */; }; 0CF93195249834DB00E1016A /* Align+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF93193249834DB00E1016A /* Align+Extensions.swift */; }; 0CF931972499039500E1016A /* AnchorAPIsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF931962499039500E1016A /* AnchorAPIsTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ 0CC3A06D1D7476A700754E59 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0CE3610F1C86077B00EA70CF /* Project object */; proxyType = 1; remoteGlobalIDString = 0CE361171C86077B00EA70CF; remoteInfo = "Arranged iOS"; }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ 0C683A0F1FBA4624002ACEB9 /* AnchorCollectionEdgesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorCollectionEdgesTests.swift; sourceTree = ""; }; 0C6C05D2287F102F009CFDE6 /* ci.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; name = ci.yml; path = .github/workflows/ci.yml; sourceTree = ""; }; 0C81ABB61C889C100036DFD4 /* Align.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Align.swift; sourceTree = ""; }; 0C8FD7781FB85E8B00A20E3D /* XCTestExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestExtensions.swift; sourceTree = ""; }; 0C8FD77E1FB85EA500A20E3D /* Diff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Diff.swift; sourceTree = ""; }; 0C8FD7811FB85F5600A20E3D /* AnchorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorTests.swift; sourceTree = ""; }; 0C8FD7841FB8608E00A20E3D /* ConstraintsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstraintsTests.swift; sourceTree = ""; }; 0C8FD78D1FB88B9B00A20E3D /* AnchorAlignmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorAlignmentTests.swift; sourceTree = ""; }; 0C8FD7901FB88BC300A20E3D /* AnchorEdgeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorEdgeTests.swift; sourceTree = ""; }; 0C8FD7931FB88BED00A20E3D /* AnchorCenterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorCenterTests.swift; sourceTree = ""; }; 0C8FD7961FB88C0600A20E3D /* AnchorDimensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorDimensionTests.swift; sourceTree = ""; }; 0C8FD7991FB88CE100A20E3D /* AnchorCollectionCenterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorCollectionCenterTests.swift; sourceTree = ""; }; 0C8FD79C1FB88D2100A20E3D /* AnchorCollectionSizeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorCollectionSizeTests.swift; sourceTree = ""; }; 0CBC06B7249E9F790055D1B1 /* AnchorPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorPerformanceTests.swift; sourceTree = ""; }; 0CC3A0671D7476A700754E59 /* Align Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Align Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 0CCC2072287E3D26001FC3CE /* Align.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = Align.docc; sourceTree = ""; }; 0CD220BF22BBED040095AD2A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 0CD220C022BBED0A0095AD2A /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; 0CD220C322BBED250095AD2A /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; 0CD220C422BBED2C0095AD2A /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = ""; }; 0CE361181C86077B00EA70CF /* Align.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Align.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 0CF93193249834DB00E1016A /* Align+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Align+Extensions.swift"; sourceTree = ""; }; 0CF931962499039500E1016A /* AnchorAPIsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorAPIsTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 0CC3A0641D7476A700754E59 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 0CC3A06C1D7476A700754E59 /* Align.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; 0CE361141C86077B00EA70CF /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 0C8FD7771FB85E7A00A20E3D /* Extensions */ = { isa = PBXGroup; children = ( 0C8FD7781FB85E8B00A20E3D /* XCTestExtensions.swift */, 0C8FD77E1FB85EA500A20E3D /* Diff.swift */, 0CF93193249834DB00E1016A /* Align+Extensions.swift */, ); path = Extensions; sourceTree = ""; }; 0CC3A0721D7476B300754E59 /* Tests */ = { isa = PBXGroup; children = ( 0C8FD7811FB85F5600A20E3D /* AnchorTests.swift */, 0CF931962499039500E1016A /* AnchorAPIsTests.swift */, 0C8FD78D1FB88B9B00A20E3D /* AnchorAlignmentTests.swift */, 0C8FD7901FB88BC300A20E3D /* AnchorEdgeTests.swift */, 0C8FD7931FB88BED00A20E3D /* AnchorCenterTests.swift */, 0C8FD7961FB88C0600A20E3D /* AnchorDimensionTests.swift */, 0C683A0F1FBA4624002ACEB9 /* AnchorCollectionEdgesTests.swift */, 0C8FD7991FB88CE100A20E3D /* AnchorCollectionCenterTests.swift */, 0C8FD79C1FB88D2100A20E3D /* AnchorCollectionSizeTests.swift */, 0CBC06B7249E9F790055D1B1 /* AnchorPerformanceTests.swift */, 0C8FD7841FB8608E00A20E3D /* ConstraintsTests.swift */, 0C8FD7771FB85E7A00A20E3D /* Extensions */, ); path = Tests; sourceTree = SOURCE_ROOT; }; 0CD220BB22BBECD70095AD2A /* Metadata */ = { isa = PBXGroup; children = ( 0CD220BF22BBED040095AD2A /* README.md */, 0CD220C022BBED0A0095AD2A /* Package.swift */, 0CD220C322BBED250095AD2A /* LICENSE */, 0CD220C422BBED2C0095AD2A /* CHANGELOG.md */, 0C6C05D2287F102F009CFDE6 /* ci.yml */, ); name = Metadata; sourceTree = ""; }; 0CE3610E1C86077B00EA70CF = { isa = PBXGroup; children = ( 0CE361261C8607D600EA70CF /* Sources */, 0CC3A0721D7476B300754E59 /* Tests */, 0CD220BB22BBECD70095AD2A /* Metadata */, 0CE361191C86077B00EA70CF /* Products */, ); sourceTree = ""; }; 0CE361191C86077B00EA70CF /* Products */ = { isa = PBXGroup; children = ( 0CE361181C86077B00EA70CF /* Align.framework */, 0CC3A0671D7476A700754E59 /* Align Tests.xctest */, ); name = Products; sourceTree = ""; }; 0CE361261C8607D600EA70CF /* Sources */ = { isa = PBXGroup; children = ( 0C81ABB61C889C100036DFD4 /* Align.swift */, 0CCC2072287E3D26001FC3CE /* Align.docc */, ); path = Sources; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ 0CE361151C86077B00EA70CF /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ 0CC3A0661D7476A700754E59 /* Align Tests */ = { isa = PBXNativeTarget; buildConfigurationList = 0CC3A06F1D7476A700754E59 /* Build configuration list for PBXNativeTarget "Align Tests" */; buildPhases = ( 0CC3A0631D7476A700754E59 /* Sources */, 0CC3A0641D7476A700754E59 /* Frameworks */, 0CC3A0651D7476A700754E59 /* Resources */, ); buildRules = ( ); dependencies = ( 0CC3A06E1D7476A700754E59 /* PBXTargetDependency */, ); name = "Align Tests"; productName = "Arranged iOS Tests"; productReference = 0CC3A0671D7476A700754E59 /* Align Tests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; }; 0CE361171C86077B00EA70CF /* Align */ = { isa = PBXNativeTarget; buildConfigurationList = 0CE361201C86077B00EA70CF /* Build configuration list for PBXNativeTarget "Align" */; buildPhases = ( 0CE361131C86077B00EA70CF /* Sources */, 0CE361141C86077B00EA70CF /* Frameworks */, 0CE361151C86077B00EA70CF /* Headers */, 0CE361161C86077B00EA70CF /* Resources */, ); buildRules = ( ); dependencies = ( ); name = Align; productName = Stack; productReference = 0CE361181C86077B00EA70CF /* Align.framework */; productType = "com.apple.product-type.framework"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 0CE3610F1C86077B00EA70CF /* Project object */ = { isa = PBXProject; attributes = { BuildIndependentTargetsInParallel = YES; LastSwiftUpdateCheck = 0800; LastUpgradeCheck = 1600; ORGANIZATIONNAME = "Alexander Grebenyuk"; TargetAttributes = { 0CC3A0661D7476A700754E59 = { CreatedOnToolsVersion = 8.0; DevelopmentTeam = NR8DLKJ7E6; LastSwiftMigration = 0910; ProvisioningStyle = Automatic; }; 0CE361171C86077B00EA70CF = { CreatedOnToolsVersion = 7.2.1; LastSwiftMigration = 0800; }; }; }; buildConfigurationList = 0CE361121C86077B00EA70CF /* Build configuration list for PBXProject "Align" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( en, Base, ); mainGroup = 0CE3610E1C86077B00EA70CF; productRefGroup = 0CE361191C86077B00EA70CF /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( 0CE361171C86077B00EA70CF /* Align */, 0CC3A0661D7476A700754E59 /* Align Tests */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ 0CC3A0651D7476A700754E59 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; 0CE361161C86077B00EA70CF /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ 0CC3A0631D7476A700754E59 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 0C8FD79A1FB88CE100A20E3D /* AnchorCollectionCenterTests.swift in Sources */, 0C8FD79D1FB88D2100A20E3D /* AnchorCollectionSizeTests.swift in Sources */, 0C8FD7941FB88BED00A20E3D /* AnchorCenterTests.swift in Sources */, 0CBC06B8249E9F790055D1B1 /* AnchorPerformanceTests.swift in Sources */, 0C8FD77F1FB85EA500A20E3D /* Diff.swift in Sources */, 0C683A111FBA462D002ACEB9 /* AnchorCollectionEdgesTests.swift in Sources */, 0C8FD7791FB85E8B00A20E3D /* XCTestExtensions.swift in Sources */, 0C8FD78E1FB88B9B00A20E3D /* AnchorAlignmentTests.swift in Sources */, 0CF931972499039500E1016A /* AnchorAPIsTests.swift in Sources */, 0CF93195249834DB00E1016A /* Align+Extensions.swift in Sources */, 0C8FD7911FB88BC300A20E3D /* AnchorEdgeTests.swift in Sources */, 0C8FD7821FB85F5600A20E3D /* AnchorTests.swift in Sources */, 0C8FD7851FB8608E00A20E3D /* ConstraintsTests.swift in Sources */, 0C8FD7971FB88C0600A20E3D /* AnchorDimensionTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 0CE361131C86077B00EA70CF /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 0CCC2073287E3D26001FC3CE /* Align.docc in Sources */, 0C81ABB71C889C100036DFD4 /* Align.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ 0CC3A06E1D7476A700754E59 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 0CE361171C86077B00EA70CF /* Align */; targetProxy = 0CC3A06D1D7476A700754E59 /* PBXContainerItemProxy */; }; /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */ 0CC3A0701D7476A700754E59 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { CLANG_WARN_SUSPICIOUS_MOVES = YES; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", ); PRODUCT_BUNDLE_IDENTIFIER = "com.github.kean.Align-iOS-Tests"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; }; name = Debug; }; 0CC3A0711D7476A700754E59 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { CLANG_WARN_SUSPICIOUS_MOVES = YES; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", ); PRODUCT_BUNDLE_IDENTIFIER = "com.github.kean.Align-iOS-Tests"; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; }; 0CE3611E1C86077B00EA70CF /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_TESTABILITY = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); GENERATE_INFOPLIST_FILE = YES; IPHONEOS_DEPLOYMENT_TARGET = 15.6; MACOSX_DEPLOYMENT_TARGET = 11.5; ONLY_ACTIVE_ARCH = YES; SUPPORTED_PLATFORMS = "iphoneos appletvos iphonesimulator appletvsimulator macosx"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_STRICT_CONCURRENCY = complete; SWIFT_VERSION = 6.0; TVOS_DEPLOYMENT_TARGET = 15.6; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Debug; }; 0CE3611F1C86077B00EA70CF /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; GENERATE_INFOPLIST_FILE = YES; IPHONEOS_DEPLOYMENT_TARGET = 15.6; MACOSX_DEPLOYMENT_TARGET = 11.5; SUPPORTED_PLATFORMS = "iphoneos appletvos iphonesimulator appletvsimulator macosx"; SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_STRICT_CONCURRENCY = complete; SWIFT_VERSION = 6.0; TVOS_DEPLOYMENT_TARGET = 15.6; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Release; }; 0CE361211C86077B00EA70CF /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { APPLICATION_EXTENSION_API_ONLY = YES; DOCC_HOSTING_BASE_PATH = /align/; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", ); MARKETING_VERSION = 4.0.0; PRODUCT_BUNDLE_IDENTIFIER = com.github.kean.Align; PRODUCT_NAME = Align; SKIP_INSTALL = YES; }; name = Debug; }; 0CE361221C86077B00EA70CF /* Release */ = { isa = XCBuildConfiguration; buildSettings = { APPLICATION_EXTENSION_API_ONLY = YES; DOCC_HOSTING_BASE_PATH = /align/; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", ); MARKETING_VERSION = 4.0.0; PRODUCT_BUNDLE_IDENTIFIER = com.github.kean.Align; PRODUCT_NAME = Align; SKIP_INSTALL = YES; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ 0CC3A06F1D7476A700754E59 /* Build configuration list for PBXNativeTarget "Align Tests" */ = { isa = XCConfigurationList; buildConfigurations = ( 0CC3A0701D7476A700754E59 /* Debug */, 0CC3A0711D7476A700754E59 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 0CE361121C86077B00EA70CF /* Build configuration list for PBXProject "Align" */ = { isa = XCConfigurationList; buildConfigurations = ( 0CE3611E1C86077B00EA70CF /* Debug */, 0CE3611F1C86077B00EA70CF /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 0CE361201C86077B00EA70CF /* Build configuration list for PBXNativeTarget "Align" */ = { isa = XCConfigurationList; buildConfigurations = ( 0CE361211C86077B00EA70CF /* Debug */, 0CE361221C86077B00EA70CF /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; rootObject = 0CE3610F1C86077B00EA70CF /* Project object */; } ================================================ FILE: Align.xcodeproj/project.xcworkspace/contents.xcworkspacedata ================================================ ================================================ FILE: Align.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist ================================================ IDEDidComputeMac32BitWarning ================================================ FILE: Align.xcodeproj/xcshareddata/xcschemes/Align.xcscheme ================================================ ================================================ FILE: CHANGELOG.md ================================================ [Changelog](https://github.com/kean/Align/releases) for all versions # Align 4 ## Align 4.0 *Apr 25, 2026* ### New APIs - Add an optional `priority:` parameter to all constraint-creating methods, allowing the priority to be set at creation time without needing to wrap the call in a `Constraints` group - Add a `LayoutPriority` typealias (`UILayoutPriority` on iOS/tvOS, `NSLayoutConstraint.Priority` on macOS) - Add an optional `relation:` parameter to the single-edge `pin(to:inset:relation:priority:)`, restoring symmetry with `spacing(_:to:relation:priority:)` - Add an optional `offset: CGPoint` parameter to `AnchorCollectionCenter.align(offset:priority:)`, restoring symmetry with the single-anchor `Anchor.align(offset:priority:)` - Add a `/` operator for `Anchor`, mirroring `*`, so dimensions can be written as `view.anchors.height / 2` - Upgrade `Constraints`'s `Collection` conformance to `RandomAccessCollection`, matching the underlying `[NSLayoutConstraint]` storage and giving callers O(1) indexing and `count` ### API Changes & Deprecations - Fix typo in public API: rename `greaterThanOrEqul(_:)` to `greaterThanOrEqual(_:)` on `AnchorCollectionSize` - Convert `AnchorCollectionEdges.absolute()` from a method to a computed property `absolute`, since it takes no arguments and has no side effects. Call sites change from `view.anchors.edges.absolute().pin()` to `view.anchors.edges.absolute.pin()` - Replace the class hierarchies inside `AnchorAxis` and `AnchorType` with empty enums plus `@_marker` protocols (`AnchorKind`, `AlignmentAnchorKind`). The phantom types are now uninstantiable and inherently `Sendable`. `AnchorType.Alignment` is replaced by `AlignmentAnchorKind` - Replace the four hand-rolled `Constraints(for:)` arity overloads with a single variadic-generics initializer using Swift 5.9 parameter packs. Existing trailing-closure call sites (`Constraints(for: a, b) { a, b in ... }`) compile unchanged; non-trailing call sites must now pass the closure with the `closure:` label - Deprecated the Core API on `AnchorCollectionEdges` (`equal(_:insets:priority:)` and `lessThanOrEqual(_:insets:priority:)`, both `EdgeInsets` and `CGFloat` overloads), which duplicated the Semantic API. Use `pin(to:insets:priority:)` and `pin(to:insets:alignment:priority:)` (with `.center`) instead ### Safety & Correctness - Replace `superview!` force-unwraps in `pin()` and `align()` with `precondition`s that report the offending view, instead of an opaque `Optional("nil")` trap - Move `@MainActor` from the `Constraints` struct itself to just the members that touch UIKit/AppKit (`init`, `activate`, `deactivate`, `add`, `install`, and the static `stack`). The `Collection` conformance no longer needs `nonisolated` + `MainActor.assumeIsolated`, eliminating a runtime trap when a `Constraints` value was iterated off the main actor and turning that misuse into a compile-time error on the constraint-building entry points instead - Move `LayoutItem`'s `superview` requirement behind `@_spi(Align)` and add an SPI-only `_disableAutoresizingMask()` requirement, letting `Constraints.add` dispatch via the witness table instead of an `as?` cast on every constraint. The default public surface is unchanged; clients that need these (e.g. for custom `LayoutItem` conformances) can opt in with `@_spi(Align) import Align` ### Performance - Make `Constraints` a struct to avoid an allocation cost - Mark hot-path `Anchor` accessors, operators, and `equal/greaterThanOrEqual/lessThanOrEqual` overloads `@inlinable` so callers in client modules can inline through them - Mark `Anchor.offsetting(by:)` and `Anchor.multiplied(by:)` as `consuming`, letting the compiler forward the existential's class reference into the returned anchor instead of retaining and releasing it - Mark the `+`, `-`, and `*` operators on `Anchor` as `consuming` their anchor operand so chains like `view.anchors.top + 10 + 4` collapse to zero extra retains - Inline the nested `constrain` helper in `AnchorCollectionEdges.pin` to eliminate per-call closure context allocation - Hoist calls to `_disableAutoresizingMask` in scenarios where it's called many times repeatedly like `edges.pin` ### Housekeeping - Remove `Align.playground` in favor of an inline `#Preview` in `Sources/Align.swift` # Align 3 ## Align 3.3 *Sep 13, 2024* - Add `Sendable` and `@MainActor` annotations - Add compatibility with Swift 6 and Xcode 16 ## Align 3.2 *April 13, 2024* - Remove deprecated `.base` property - Increase the minimum supported Xcode version to 14.3 - Increase the minimum supported platforms to iOS 13.0, tvOS 13.0, macOS 10.15 ## Align 3.1.0 *July 22, 2023* - Use static linking by default - Fix warnings in Xcode 15 ## Align 3.0.0 *July 13, 2022* - **Breaking Change**: The `Alignment` used in `pin()` method now works slightly differently by default for the pre-defined `.trailing`, `.leading`, `.bottom`, and `.top` alignments. Previously, `.leading` alignment would pin to the view to the `.leading` horizontal guide and `.fill` the view vertically. In Align 3.0, it centers the view vertically instead. The same logic is applied to other previously listed alignments. - Add new [documentation](https://kean-docs.github.io/align/documentation/align/) created using DocC - Increase the minimum required Xcode version to 13.3 - Increase the minimum supported platforms to iOS 12.0 / tvOS 12.0 / macOS 10.14 - Rename `LayoutAnchors/base` to `LayoutAnchors/item` - Move `Alignment` to `AnchorCollectionEdges/Alignment ` - Fix typos # Align 2 ## Align 2.4.1 *June 21, 2020* - Fix typo in `Alignment` ## Align 2.4.0 *June 21, 2020* - Add [Cheat Sheet](https://github.com/kean/Align/files/4809887/align-cheat-sheet.pdf) - Remove `anchors.margins` and `anchors.safeArea` APIs - Documentation improvements ## Align 2.3.0 *June 20, 2020* - `Constraints` type now conforms to `Collection` protocol (backed by `Array`) - Add `clamp(to limit: ClosedRange)` API for dimension anchors - Add `Constraints` `activate()` and `deactivate()` methods - Add default `insets` argument for `AnchorCollectionEdges` `equal` method - Replace the target parameter of `AnchorCollectionEdges` `equal` method with `LayoutItem` - Add `AnchorCollectionEdges` variant that works with `CGFloat` - Add `AnchorCollectionEdges` `lessThatOrEqual()` method - Fix `AnchorCollectionCenter` `lessThatOrEqual()` method - Replace the target parameter of `AnchorCollectionCenter` Core APImethod with `LayoutItem` - Performance optimizations ## Align 2.2.1 *June 18, 2020* - Add a missing version of `pin()` that works with `CGFloat` as insets ## Align 2.2.0 *June 18, 2020* - Add missing Core APIs for collections ## Align 2.1.0 *June 17, 2020* > Use [Migration Guide](https://github.com/kean/Align/blob/master/Docs/MigrationGuide2.md) included in the repo to ease migration. - Remove all deprecated APIs. If you are migrating from the previous version, consider migrating to version 2.0.0 first. It is going to guide you through the migration. ## Align 2.0.0 *June 17, 2020* > Use [Migration Guide](https://github.com/kean/Align/blob/master/Docs/MigrationGuide2.md) included in the repo to ease migration. - Add `macOS support` - Add new low-level APIs: `equal`, `greaterThanOrEqual`, `lessThatOrEqual` - Add `spacing()` method for alignments - Rename `.al` to `.anchors` - Remove `.al` version accepting closure - Add `constraints` property to `Constraints` type to allow access to all of the constraints created using it - Add `activate` parameter to `Constraints` initiliazer to optionally disable automatic activation of constraints - Deprecated `func edges(_ edges: LayoutEdge...)`, use `pin(axis:)` instead - `pin()` methods now use `.leading` and `.trailing` anchors instead of absolute `.left` and `.right` anchors. To switch to absolute anchors, use `absolute()`: `view.anchors.edges.absolute()` - Remove `addSubview` family of APIs - Migrate to Swift 5.1 - Increase minimum required platform versions # Align 1 ## Align 1.2.1 - Add support for Swift Package Manager 5.0 ## Align 1.2 - Rebrand ## Align 1.1 - Add Swift 5.0 support - Remove Swift 4.0 and Swift 4.1 support - Remove iOS 9, tvOS 9 support ## Align 1.0 Updated to Swift 4.2 (required) and Xcode 10. ## Align 0.6 A minor update to make Align a bit more ergonomic. - Add iOS 9 and tvOS 9 compatibility (used to require iOS 10 and tvOS 10). - Add operators for setting multipliers: `subtitle.height.match(title.height * 2)`. - Deprecated `align(with:)` and `match(:)` method that had `offset` and `multiplier` parameters. Operators are the preferred way to set those (more compact and more obvious what they mean). - Move phantom types (e.g. `AnchorAxisVertical`) into namespaces to reduce clutter in a global namespace. ## Align 0.5.1 - Improve documentation - Improve some minor implementation details - Update project to Xcode 9.3 recommended settings ## Align 0.5 - Remove Stacks and Spacers ([gist](https://gist.github.com/kean/e77bac3625124b1de559a241a72d1e09)) - Remove Insets ## Align 0.4 - `pinToSuperviewMargins` methods now use margin attributes (e.g. `.topMargin`) and not `layoutMarginsGuide` to workaround issues on iOS 10 where layout guides are unpredictable https://stackoverflow.com/questions/32694124/auto-layout-layoutmarginsguide - Add `pinToSafeArea(of:)` family of methods which use `safeAreaLayoutGuide` on iOS 11 and fall back to `topLayoutGuide` and `bottomLayoutGuide` on iOS 10 - `addSubview` methods are no longer generic which allows for more extra flexibility when adding constraints (e.g. you can create and operate on an array of layout proxies) ## Align 0.3.1 Small update that focuses on improving `offsetting(by:)` method. - `offsetting(by:)` method now available for all anchors - Add an operator that wraps `offsetting(by:)` method (which wasn't very convenient by itself) - [Fix] Offsetting anchor which already has an offset now works correctly - Split the project into two files ## Align 0.3 - With new `addSubview(_:constraints:)` method you define a view hierarchy and layout views at the same time. It encourages splitting layout code into logical blocks and prevents programmer errors (e.g. trying to add constraints to views not in view hierarchy). - Remove standalone `fillSuperview(..)` and `centerInSuperview()` family of functions. There were multiple cons of having them (e.g. more terminology to learn, hard to document and explain, inconsistent with `center` and `size` manipulations, were not allowing to pin in a corner). Now you can manipulate multiple edges at the same time instead: ```swift view.addSubview(stack) { $0.edges.pinToSuperview() // pins the edges to fill the superview $0.edges.pinToSuperview(insets: Insets(10)) // with insets $0.edges.pinToSuperviewMargins() // or margins $0.edges(.left, .right).pinToSuperview() // fill along horizontal axis $0.centerY.alignWithSuperview() // center along vertical axis } ``` This is a much simpler model which removes entire layer of standalone methods available on `LayoutItems`. Now you always select either an `anchor` or `collections of anchors`, then use their methods to add constraints. Much simpler. - Make LayoutAnchors's `base` public to enable adding custom extensions on top of it. ## Align 0.2 - Redesigned Align API which now follow [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). Although most of the APIs are compact, it is a *non-goal* to enable the most concise syntax possible. Instead Align provides a fluent APIs that form grammatical phrases. - Full test coverage - Add a new comprehensive overview, [full guide](https://github.com/kean/Align/blob/master/Docs/AlignGuide.md), and [installation guide](https://github.com/kean/Align/blob/master/Docs/InstallationGuide.md) ## Align 0.1.1 - Revert to original `Spacer` design ## Align 0.1 - Initial version ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2017-2026 Alexander Grebenyuk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ .PHONY: help docs preview-docs clean-docs test help: @echo "Targets:" @echo " docs Build self-hostable DocC site into .build/docs" @echo " preview-docs Serve docs locally with docc preview" @echo " clean-docs Remove generated docs and DocC derived data" @echo " test Run the test suite via swift test" @echo "" @echo "Overrides for 'docs' (env vars):" @echo " HOSTING_BASE_PATH=Align URL prefix; set to '' for domain root" @echo " OUTPUT_DIR=.build/docs Output directory" @echo " DESTINATION='generic/platform=iOS' xcodebuild destination" docs: @./.scripts/build-docs.sh preview-docs: @xcodebuild -scheme Align -destination 'generic/platform=iOS' \ -derivedDataPath .build/docc-derived-data \ docbuild OTHER_DOCC_FLAGS="--preview" clean-docs: rm -rf .build/docs .build/docc-derived-data test: swift test ================================================ FILE: Package.swift ================================================ // swift-tools-version:6.0 import PackageDescription let package = Package( name: "Align", platforms: [ .iOS(.v15), .tvOS(.v15), .macOS(.v13), .visionOS(.v1) ], products: [ .library(name: "Align", targets: ["Align"]), ], targets: [ .target(name: "Align", path: "Sources"), .testTarget(name: "AlignTests", dependencies: ["Align"], path: "Tests") ] ) ================================================ FILE: README.md ================================================ ![logo](https://user-images.githubusercontent.com/1567433/178810472-8b5f687e-ed7f-491c-99ed-e86e563462ef.png)

The best way to create constraints in code. - **Semantic**. Align APIs focus on your goals, not the math behind Auto Layout constraints. - **Powerful**. Create multiple constraints with a single line of code. - **Type Safe**. Makes it impossible to create invalid constraints, at compile time. - **Fluent**. Concise and clear API that follows [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). - **Simple**. Stop worrying about `translatesAutoresizingMaskIntoConstraints` and constraints activation. The same constraint, with and without Align: ```swift // NSLayoutAnchor view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 20), view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 20), view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: -20), view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -20) ]) // Align view.anchors.edges.pin(insets: 20) ``` More examples: ```swift // Core API view.anchors.top.equal(superview.top) view.anchors.width.equal(view.anchors.height * 2) // Semantic API view.anchors.edges.pin(to: superview.safeAreaLayoutGuide, insets: 20) view.anchors.width.clamp(to: 10...40) ``` And here's something a bit more powerful: ```swift view.anchors.edges.pin(insets: 20, alignment: .center) ``` pin edges with center alignment ## Tip: Avoid Repeating `import Align` To make Align available throughout your module without writing `import Align` in every file, add a single file (e.g. `Align+Exported.swift`) containing: ```swift @_exported import Align ``` Any file in the same module can then use Align's APIs directly. This is especially convenient in app targets that lean heavily on Auto Layout. ## Documentation The [**documentation**](https://kean-docs.github.io/align/documentation/align/) for Align is created using DocC and covers all of its APIs in a clear visual way. There is also a [**cheat sheet**](https://github.com/kean/Align/blob/master/Sources/Align.docc/Resources/align-cheat-sheet.pdf) available that lists all of the available APIs. Screen Shot 2022-07-13 at 10 08 57 AM ## Requirements | Align | Swift | Xcode | Platforms | |----------------|-------------|-------------------|--------------------------------------------| | Align 4.0 | Swift 6.0 | Xcode 26.0 | iOS 15, tvOS 15, macOS 13, visionOS 1 | | Align 3.3 | Swift 5.10 | Xcode 15.3 | iOS 14, tvOS 14, macOS 10.16 | ## Why Align Align strives for clarity and simplicity by following [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). Although most of the APIs are compact, it is a *non-goal* to enable the most concise syntax possible. What you get with Align: - **Fluent, high-level API** for everyday Auto Layout - **Lightweight** – ~400 lines, zero dependencies - **Minimal surface area** – a single new property, `anchors`, on every view and layout guide - **Fast to compile** – built on `NSLayoutConstraint`, with no operator-overload soup - **No ceremony** – constraints are activated for you, and `translatesAutoresizingMaskIntoConstraints` is handled automatically ================================================ FILE: Sources/Align+Preview.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif #if DEBUG && (os(iOS) || os(tvOS)) import SwiftUI @available(iOS 17.0, tvOS 17.0, *) #Preview { // A scrolling gallery of the highest-value Align layouts, one per titled card. let cards = UIStackView() cards.axis = .vertical cards.spacing = 24 cards.layoutMargins = UIEdgeInsets(top: 24, left: 16, bottom: 24, right: 16) cards.isLayoutMarginsRelativeArrangement = true // The bread-and-butter Align idiom: fill the container with uniform insets. cards.addArrangedSubview(demoCard("Pin edges with insets") { canvas in let box = UIView(); box.backgroundColor = .systemBlue canvas.addSubview(box) box.anchors.edges.pin(insets: 16) }) // `pin` also accepts an `alignment` to anchor a fixed-size view to a corner. cards.addArrangedSubview(demoCard("Pin to a corner") { canvas in let button = UIView(); button.backgroundColor = .systemIndigo canvas.addSubview(button) button.anchors.size.equal(CGSize(width: 96, height: 40)) button.anchors.edges.pin(insets: 16, alignment: .bottomTrailing) }) // Center the view and give it an explicit size — a classic badge/dialog layout. cards.addArrangedSubview(demoCard("Center with fixed size") { canvas in let box = UIView(); box.backgroundColor = .systemPink canvas.addSubview(box) box.anchors.center.align() box.anchors.size.equal(CGSize(width: 120, height: 44)) }) // Anchor arithmetic: pin horizontally, then drive height from width for a 2:1 banner. cards.addArrangedSubview(demoCard("Aspect ratio with arithmetic") { canvas in let banner = UIView(); banner.backgroundColor = .systemTeal canvas.addSubview(banner) banner.anchors.edges.pin(insets: 16, axis: .horizontal) banner.anchors.centerY.align() banner.anchors.height.equal(banner.anchors.width * 0.5) }) let scroll = UIScrollView() scroll.backgroundColor = .systemBackground scroll.addSubview(cards) // Match the content width to the scroll view so it scrolls vertically only. cards.anchors.edges.pin() cards.anchors.width.equal(scroll.anchors.width) return scroll } @MainActor private func demoCard(_ title: String, _ build: (UIView) -> Void) -> UIView { let label = UILabel() label.font = .systemFont(ofSize: 13, weight: .semibold) label.textColor = .secondaryLabel label.text = title let canvas = UIView() canvas.backgroundColor = .secondarySystemBackground canvas.anchors.height.equal(140) build(canvas) let stack = UIStackView(arrangedSubviews: [label, canvas]) stack.axis = .vertical; stack.spacing = 6 return stack } #endif ================================================ FILE: Sources/Align.docc/Align.md ================================================ # ``Align`` The best way to create constraints in code. ![Logo](logo.png) - **Semantic**. Align APIs focus on your goals, not the math behind Auto Layout constraints. - **Powerful**. Create multiple constraints with a single line of code. - **Type Safe**. Makes it impossible to create invalid constraints, at compile time. - **Fluent**. Concise and clear API that follows [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). - **Simple**. Stop worrying about `translatesAutoresizingMaskIntoConstraints` and constraints activation. To give you a taste of what the *semantic* APIs look like, here is an example: ```swift view.anchors.edges.pin(insets: 20, alignment: .center) ``` ![01](01.png) ## Getting Started The entire library fits in a single file with around 330 lines of code. The best way to install it is by using Swift Package Manager, but you can also simply drag-n-drop it into your app. If you install it as a package, you can avoid having to import it in every file by re-exposing its APIs in your app target: ```swift import Align extension LayoutItem { var anchors: Align.LayoutAnchors { Align.LayoutAnchors(self) } } ``` ## Overview The Align APIs for creating constraints fall into two categories: ![02](02.png) **Core API** lets you create constraints by setting relations between one or more anchors. These APIs are similar to what `NSLayoutAnchor` provides. **Semantic API** is a high-level API that focuses on your goals, such as pinning edges to the container, aligning the view, setting spacing between views, etc. Both APIs are designed to be easily discoverable using Xcode code completions. There is also a [**cheat sheet**](https://github.com/kean/Align/blob/master/Sources/Align.docc/Resources/align-cheat-sheet.pdf) available listing all APIs in one place. ## Requirements | Align | Swift | Xcode | Platforms | |----------------|-------------|-------------------|--------------------------------------------| | Align 4.0 | Swift 6.0 | Xcode 16 | iOS 15, tvOS 15, macOS 13, visionOS 1 | | Align 3.3 | Swift 5.10 | Xcode 15.3 | iOS 14, tvOS 14, macOS 10.16 | ## Topics ### Essentials - ``LayoutItem`` - ``LayoutAnchors`` - ``Anchor`` ### Manipulating Multiple Anchors - ``AnchorCollectionEdges`` - ``AnchorCollectionCenter`` - ``AnchorCollectionSize`` ### Managing Constraints - ``Constraints`` ================================================ FILE: Sources/Align.docc/Alignment+Extension.md ================================================ # ``Align/AnchorCollectionEdges/Alignment`` ## Topics ### Initializers - ``init(horizontal:vertical:)`` ### Predefined Alignments - ``fill`` - ``center`` - ``topLeading`` - ``top`` - ``topTrailing`` - ``trailing`` - ``bottomTrailing`` - ``bottom`` - ``bottomLeading`` - ``leading`` ================================================ FILE: Sources/Align.docc/Anchor+Extensions.md ================================================ # ``Align/Anchor`` ### Core Constraints ```swift // Align two views along one of the edges a.anchors.leading.equal(b.anchors.leading) // Other options are available: // a.anchors.leading.greaterThanOrEqual(b.anchors.leading) // a.anchors.leading.greaterThanOrEqual(b.anchors.leading, constant: 10) // Set height to the given value (CGFloat) a.anchors.height.equal(30) ``` ![03](03.png) > tip: Align automatically sets `translatesAutoresizingMaskIntoConstraints` to `false` for every view that you manipulate using it, so you no longer have to worry about it. Align also allows you to offset, multiply, and divide anchors. This is a lightweight operation with no allocations involved. ```swift // Offset one of the anchors, creating a "virtual" anchor b.anchors.leading.equal(a.anchors.trailing + 20) // Set aspect ratio for a view b.anchors.height.equal(a.anchors.width * 2) // Divide an anchor — equivalent to multiplying by the reciprocal b.anchors.height.equal(a.anchors.height / 2) ``` ![04](04.png) ### Semantic Constraints ```swift // Set spacing between two views a.anchors.bottom.spacing(20, to: b.anchors.top) // Pin an edge to the superview a.anchors.trailing.pin(inset: 20) ``` ![05](05.png) ```swift // Clamps the dimension of a view to the given limiting range. a.anchors.width.clamp(to: 40...100) ``` ![06](06.png) ### Setting a Priority Every constraint-creating method accepts an optional `priority:` parameter. When `nil` (the default), the system's default priority is used. ```swift // Set a required width, with a fallback maximum a.anchors.width.equal(100, priority: .defaultHigh) a.anchors.width.lessThanOrEqual(200) ``` ## Topics ### Core Constraints for Edges and Center These constraints can be added between anchors that represent view edges and center, but only if they operate in the same axis. - ``equal(_:constant:priority:)-(Anchor,_,_)`` - ``greaterThanOrEqual(_:constant:priority:)-(Anchor,_,_)`` - ``lessThanOrEqual(_:constant:priority:)-(Anchor,_,_)`` ### Core Constraints for Dimensions - ``equal(_:priority:)`` - ``greaterThanOrEqual(_:priority:)`` - ``lessThanOrEqual(_:priority:)`` - ``equal(_:constant:priority:)-(Anchor,_,_)`` - ``greaterThanOrEqual(_:constant:priority:)-(Anchor,_,_)`` - ``lessThanOrEqual(_:constant:priority:)-(Anchor,_,_)`` ### Core Constraints - ``offsetting(by:)`` - ``multiplied(by:)`` ### Semantic Constraints for Edges - ``pin(to:inset:relation:priority:)`` - ``spacing(_:to:relation:priority:)`` ### Semantic Constraints for Dimensions - ``clamp(to:priority:)`` ### Semantic Constraints for Center - ``align(offset:priority:)`` ### Anchor Parameters - ``AnchorAxis`` - ``AnchorType`` ================================================ FILE: Sources/Align.docc/AnchorCollectionCenter+Extension.md ================================================ # ``Align/AnchorCollectionCenter`` ```swift a.anchors.center.align() ``` ![Center](center-01.png) ## Topics ### Core Constraints - ``equal(_:offset:priority:)`` - ``lessThanOrEqual(_:offset:priority:)`` - ``greaterThanOrEqual(_:offset:priority:)`` ### Semantic Constraints - ``align(offset:priority:)`` - ``align(with:priority:)`` ================================================ FILE: Sources/Align.docc/AnchorCollectionEdges+Extension.md ================================================ # ``Align/AnchorCollectionEdges`` ### Pin Edges The main API available for edges is ``pin(to:insets:axis:alignment:priority:)-(_,EdgeInsets,_,_,_)`` that pins the edges to the container. ```swift // A convenience method: view.anchors.edges.pin(insets: 20) // Same output as the following: view.anchors.edges.pin( to: view.superview!, insets: EdgeInsets(top: 20, left: 20, bottom: 20, right: 20), alignment: .fill ) ``` ![Edges](edges-01.png) By default, it pins the edges to the superview of the current view. However, you can select any view as a container or even a layout guide. ```swift // Pin to superview view.anchors.edges.pin() // Pin to layout margins guide view.anchors.edges.pin(to: container.layoutMarginsGuide) // Pin to safe area view.anchors.edges.pin(to: container.safeAreaLayoutGuide) ``` ### Pin with Alignment By default, ``pin(to:insets:axis:alignment:priority:)-(_,EdgeInsets,_,_,_)`` uses alignment ``Alignment/fill``, but there are many other alignments available. For example, with ``Alignment/center``, the view is centered in the container and the edges are pinned with "less than or equal" constraints making sure it doesn't overflow the container. ```swift view.anchors.edges.pin(insets: 20, alignment: .center) ``` ![Edges](edges-02.png) Another useful alignment is ``Alignment/topLeading`` that pins the view to the corner. ```swift view.anchors.edges.pin(insets: 20, alignment: .topLeading) ``` ![Edges](edges-04.png) In addition to the predefined alignments such as ``Alignment/fill`` and ``Alignment/topLeading``, you can also create custom alignments by providing a vertical and horizontal component separately. ```swift anchors.edges.pin( insets: 20, alignment: Alignment(vertical: .center, horizontal: .leading) ) ``` ![Edges](edges-05.png) ### Constraints Along the Axis Sometimes, you just need to create constraints along the given axis and you can do that using the `axis` parameter. ```swift view.anchors.edges.pin(insets: 20, axis: .horizontal, alignment: .center) ``` ![Edges](edges-03.png) ## Topics ### Semantic Constraints - ``pin(to:insets:axis:alignment:priority:)-(_,EdgeInsets,_,_,_)`` - ``pin(to:insets:axis:alignment:priority:)-(_,CGFloat,_,_,_)`` ### Instance Properties - ``absolute`` ### Deprecated - ``equal(_:insets:priority:)-(_,EdgeInsets,_)`` - ``equal(_:insets:priority:)-(_,CGFloat,_)`` - ``lessThanOrEqual(_:insets:priority:)-(_,EdgeInsets,_)`` - ``lessThanOrEqual(_:insets:priority:)-(_,CGFloat,_)`` ### Nested Types - ``Alignment`` - ``Axis`` ================================================ FILE: Sources/Align.docc/AnchorCollectionSize+Extension.md ================================================ # ``Align/AnchorCollectionSize`` ```swift a.anchors.size.equal(CGSize(width: 120, height: 40)) ``` ![size](size-01.png) ```swift a.anchors.size.equal(b) ``` ![size](size-02.png) ## Topics ### Core Constraints - ``equal(_:priority:)`` - ``equal(_:insets:multiplier:priority:)`` - ``lessThanOrEqual(_:priority:)`` - ``lessThanOrEqual(_:insets:multiplier:priority:)`` - ``greaterThanOrEqual(_:priority:)`` - ``greaterThanOrEqual(_:insets:multiplier:priority:)`` ================================================ FILE: Sources/Align.docc/Constraints+Extension.md ================================================ # ``Align/Constraints`` By default, Align automatically activates created constraints. Using `Constraints` API, constraints are activated all at the same time when you exit from the closure. It gives you a chance to change the `priority` of the constraints. ```swift Constraints(for: title, subtitle) { title, subtitle in // Align one anchor with another subtitle.top.spacing(10, to: title.bottom + 10) // Manipulate dimensions title.width.equal(100) // Change a priority of constraints inside a group: subtitle.bottom.pin().priority = UILayoutPriority(999) } ``` > tip: Every constraint-creating method also accepts an optional `priority:` parameter, so the priority can be set inline without wrapping the call in a `Constraints` group: > > ```swift > subtitle.anchors.bottom.pin(priority: .init(999)) > ``` `Constraints` also give you easy access to Align anchors (notice, there is no `.anchors` call in the example). And if you want to not activate the constraints, there is an option for that: ```swift Constraints(activate: false) { // Create your constraints here } ``` ## Topics ### Initializers - ``init(activate:_:)`` ### Variadic Initializers The following initializer provides convenient access to the views and their anchors. It uses Swift parameter packs, so it works for any number of items. - ``init(for:closure:)`` ### Accessing Underlying Constraints - ``constraints`` - ``activate()`` - ``deactivate()`` ### Collection Conformance - ``startIndex`` - ``endIndex`` - ``index(after:)`` ================================================ FILE: Sources/Align.docc/LayoutAnchors+Extension.md ================================================ # ``Align/LayoutAnchors`` ## Topics ### Edges - ``top`` - ``bottom`` - ``left`` - ``right`` - ``leading`` - ``trailing`` ### Center - ``centerX`` - ``centerY`` ### Baselines - ``firstBaseline`` - ``lastBaseline`` ### Dimensions - ``width`` - ``height`` ### Collections - ``edges`` - ``center`` - ``size`` ### Accessing Underlying View - ``item`` ================================================ FILE: Sources/Anchor.swift ================================================ ///// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// A type that represents a layout axis. public enum AnchorAxis { public enum Horizontal {} public enum Vertical {} } /// A marker protocol identifying the kind of attribute an `Anchor` represents. @_marker public protocol AnchorKind {} /// A marker protocol for anchor kinds that can be aligned with one another /// (edges, centers, and baselines). @_marker public protocol AlignmentAnchorKind: AnchorKind {} /// Represents an anchor type. public enum AnchorType { public enum Edge: AlignmentAnchorKind {} public enum Center: AlignmentAnchorKind {} public enum Baseline: AlignmentAnchorKind {} public enum Dimension: AnchorKind {} } /// An anchor represents one of the view's layout attributes. /// /// Instead of creating `NSLayoutConstraint` objects directly, start with a `UIView` /// or `UILayoutGuide` and select one of its anchors. For example, `view.anchors.top` /// is represented by `Anchor`. Then use the /// anchor’s methods to construct your constraint. /// /// ```swift /// // Align two views along one of the edges /// a.anchors.leading.equal(b.anchors.leading) /// ``` /// /// When you create constraints using `Anchor` APIs, the constraints are activated /// automatically and the target view has `translatesAutoresizingMaskIntoConstraints` /// set to `false`. If you want to activate all the constraints at the same time, /// or create them without activation, use `Constraints` type. /// /// - tip: `UIView` does not provide anchor properties for the layout margin attributes. /// Instead, call `view.layoutMarginsGuide.anchors`. @MainActor public struct Anchor { // type and axis are phantom types @usableFromInline let item: LayoutItem @usableFromInline let attribute: NSLayoutConstraint.Attribute @usableFromInline let offset: CGFloat @usableFromInline let multiplier: CGFloat @inlinable init(_ item: LayoutItem, _ attribute: NSLayoutConstraint.Attribute, offset: CGFloat = 0, multiplier: CGFloat = 1) { self.item = item; self.attribute = attribute; self.offset = offset; self.multiplier = multiplier } /// Returns a new anchor offset by a given amount. /// /// - tip: Consider using a convenience operator instead: `view.anchors.top + 10`. @inlinable public consuming func offsetting(by offset: CGFloat) -> Anchor { Anchor(item, attribute, offset: self.offset + offset, multiplier: self.multiplier) } /// Returns a new anchor with a constant multiplied by the given amount. /// /// - tip: Consider using a convenience operator instead: `view.anchors.height * 2`. @inlinable public consuming func multiplied(by multiplier: CGFloat) -> Anchor { Anchor(item, attribute, offset: self.offset * multiplier, multiplier: self.multiplier * multiplier) } } /// Returns a new anchor offset by a given amount. @MainActor @inlinable public func + (anchor: consuming Anchor, offset: CGFloat) -> Anchor { anchor.offsetting(by: offset) } /// Returns a new anchor offset by a given amount. @MainActor @inlinable public func - (anchor: consuming Anchor, offset: CGFloat) -> Anchor { anchor.offsetting(by: -offset) } /// Returns a new anchor with a constant multiplied by the given amount. @MainActor @inlinable public func * (anchor: consuming Anchor, multiplier: CGFloat) -> Anchor { anchor.multiplied(by: multiplier) } /// Returns a new anchor with a constant divided by the given amount. @MainActor @inlinable public func / (anchor: consuming Anchor, divisor: CGFloat) -> Anchor { anchor.multiplied(by: 1 / divisor) } // MARK: - Anchors (AlignmentAnchorKind) @MainActor public extension Anchor where Type: AlignmentAnchorKind { @inlinable @discardableResult consuming func equal(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .equal, priority: priority) } @inlinable @discardableResult consuming func greaterThanOrEqual(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .greaterThanOrEqual, priority: priority) } @inlinable @discardableResult consuming func lessThanOrEqual(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .lessThanOrEqual, priority: priority) } } // MARK: - Anchors (AnchorType.Dimension) @MainActor public extension Anchor where Type == AnchorType.Dimension { @inlinable @discardableResult consuming func equal(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .equal, priority: priority) } @inlinable @discardableResult consuming func greaterThanOrEqual(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .greaterThanOrEqual, priority: priority) } @inlinable @discardableResult consuming func lessThanOrEqual(_ anchor: consuming Anchor, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, anchor, constant: constant, relation: .lessThanOrEqual, priority: priority) } @inlinable @discardableResult consuming func equal(_ constant: CGFloat, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(item: item, attribute: attribute, relatedBy: .equal, constant: constant, priority: priority) } @inlinable @discardableResult consuming func greaterThanOrEqual(_ constant: CGFloat, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(item: item, attribute: attribute, relatedBy: .greaterThanOrEqual, constant: constant, priority: priority) } @inlinable @discardableResult consuming func lessThanOrEqual(_ constant: CGFloat, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(item: item, attribute: attribute, relatedBy: .lessThanOrEqual, constant: constant, priority: priority) } /// Clamps the dimension of a view to the given limiting range. @inlinable @discardableResult consuming func clamp(to limits: ClosedRange, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [(copy self).greaterThanOrEqual(limits.lowerBound, priority: priority), lessThanOrEqual(limits.upperBound, priority: priority)] } } // MARK: - Anchors (AnchorType.Edge) // Public surface below is `@inlinable` so anchor expressions can be optimized // across module boundaries. SPI access (`superview`, `_disableAutoresizingMask`) // is funneled through `@usableFromInline` helpers (`_requireSuperview`, // `Constraints.add/install`) whose bodies stay inside the framework. @MainActor public extension Anchor where Type == AnchorType.Edge { /// Pins the edge to the respected edges of the given container. @inlinable @discardableResult consuming func pin(to container: LayoutItem? = nil, inset: CGFloat = 0, relation: NSLayoutConstraint.Relation = .equal, priority: LayoutPriority? = nil) -> NSLayoutConstraint { let isInverted: Bool = switch attribute { case .trailing, .right, .bottom: true default: false } let target = container ?? item._requireSuperview() return Constraints.add(self, toItem: target, attribute: attribute, constant: (isInverted ? -inset : inset), relation: isInverted ? relation.inverted : relation, priority: priority) } /// Adds spacing between the current anchors. @inlinable @discardableResult consuming func spacing(_ spacing: CGFloat, to anchor: consuming Anchor, relation: NSLayoutConstraint.Relation = .equal, priority: LayoutPriority? = nil) -> NSLayoutConstraint { let isInverted = (attribute == .bottom && anchor.attribute == .top) || (attribute == .right && anchor.attribute == .left) || (attribute == .trailing && anchor.attribute == .leading) return Constraints.add(self, anchor, constant: isInverted ? -spacing : spacing, relation: isInverted ? relation.inverted : relation, priority: priority) } } // MARK: - Anchors (AnchorType.Center) @MainActor public extension Anchor where Type == AnchorType.Center { /// Aligns the axis with a superview axis. @inlinable @discardableResult consuming func align(offset: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { Constraints.add(self, toItem: item._requireSuperview(), attribute: attribute, constant: offset, priority: priority) } } ================================================ FILE: Sources/AnchorCollectionCenter.swift ================================================ ///// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// Create multiple constraints at once by using both `centerX` and `centerY` anchors. @MainActor public struct AnchorCollectionCenter { @usableFromInline let x: Anchor @usableFromInline let y: Anchor @usableFromInline init(x: Anchor, y: Anchor) { self.x = x self.y = y } // MARK: Core API @inlinable @discardableResult public func equal(_ item2: Item, offset: CGPoint = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [x.equal(item2.anchors.centerX, constant: offset.x, priority: priority), y.equal(item2.anchors.centerY, constant: offset.y, priority: priority)] } @inlinable @discardableResult public func greaterThanOrEqual(_ item2: Item, offset: CGPoint = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [x.greaterThanOrEqual(item2.anchors.centerX, constant: offset.x, priority: priority), y.greaterThanOrEqual(item2.anchors.centerY, constant: offset.y, priority: priority)] } @inlinable @discardableResult public func lessThanOrEqual(_ item2: Item, offset: CGPoint = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [x.lessThanOrEqual(item2.anchors.centerX, constant: offset.x, priority: priority), y.lessThanOrEqual(item2.anchors.centerY, constant: offset.y, priority: priority)] } // MARK: Semantic API /// Centers the view in the superview. @inlinable @discardableResult public func align(offset: CGPoint = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [x.align(offset: offset.x, priority: priority), y.align(offset: offset.y, priority: priority)] } /// Makes the axes equal to the other collection of axes. @inlinable @discardableResult public func align(with item: Item, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [x.equal(item.anchors.centerX, priority: priority), y.equal(item.anchors.centerY, priority: priority)] } } ================================================ FILE: Sources/AnchorCollectionEdges.swift ================================================ ///// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// Create multiple constraints at once by operating more than one edge at once. @MainActor public struct AnchorCollectionEdges { @usableFromInline let item: LayoutItem @usableFromInline var isAbsolute = false @usableFromInline init(item: LayoutItem, isAbsolute: Bool = false) { self.item = item self.isAbsolute = isAbsolute } /// Use `left` and `right` edges instead of `leading` and `trailing`. @inlinable public var absolute: AnchorCollectionEdges { AnchorCollectionEdges(item: item, isAbsolute: true) } #if os(iOS) || os(tvOS) public typealias Axis = NSLayoutConstraint.Axis #else public typealias Axis = NSLayoutConstraint.Orientation #endif // MARK: Core API (Deprecated) @available(*, deprecated, message: "Use pin(to:insets:priority:) instead") @inlinable @discardableResult public func equal(_ item2: LayoutItem, insets: EdgeInsets = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { pin(to: item2, insets: insets, priority: priority) } @available(*, deprecated, message: "Use pin(to:insets:alignment:priority:) with alignment .center instead") @inlinable @discardableResult public func lessThanOrEqual(_ item2: LayoutItem, insets: EdgeInsets = .zero, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { _pin(to: item2, insets: insets, axis: nil, alignment: .center, isCenteringEnabled: false, priority: priority) } @available(*, deprecated, message: "Use pin(to:insets:priority:) instead") @inlinable @discardableResult public func equal(_ item2: LayoutItem, insets: CGFloat, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { pin(to: item2, insets: EdgeInsets(top: insets, left: insets, bottom: insets, right: insets), priority: priority) } @available(*, deprecated, message: "Use pin(to:insets:alignment:priority:) with alignment .center instead") @inlinable @discardableResult public func lessThanOrEqual(_ item2: LayoutItem, insets: CGFloat, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { _pin(to: item2, insets: EdgeInsets(top: insets, left: insets, bottom: insets, right: insets), axis: nil, alignment: .center, isCenteringEnabled: false, priority: priority) } // MARK: Semantic API /// Pins the edges to the edges of the given item. By default, pins the edges /// to the superview. /// /// - parameter item2: The target view, by default, uses the superview. /// - parameter insets: Insets the receiver's edges by the given insets. /// - parameter axis: If provided, creates constraints only along the given /// axis. For example, if you pass axis `.horizontal`, only the `.leading`, /// `.trailing` (and `.centerX` if needed) attributes are used. `nil` by default /// - parameter alignment: `.fill` by default, see `Alignment` for a list of /// the available options. /// - parameter priority: The priority of the created constraints. Uses the /// system's default priority when `nil`. @inlinable @discardableResult public func pin(to item2: LayoutItem? = nil, insets: CGFloat, axis: Axis? = nil, alignment: Alignment = .fill, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { pin(to: item2, insets: EdgeInsets(top: insets, left: insets, bottom: insets, right: insets), axis: axis, alignment: alignment, priority: priority) } /// Pins the edges to the edges of the given item. By default, pins the edges /// to the superview. /// /// - parameter item2: The target view, by default, uses the superview. /// - parameter insets: Insets the receiver's edges by the given insets. /// - parameter axis: If provided, creates constraints only along the given /// axis. For example, if you pass axis `.horizontal`, only the `.leading`, /// `.trailing` (and `.centerX` if needed) attributes are used. `nil` by default /// - parameter alignment: `.fill` by default, see `Alignment` for a list of /// the available options. /// - parameter priority: The priority of the created constraints. Uses the /// system's default priority when `nil`. @inlinable @discardableResult public func pin(to item2: LayoutItem? = nil, insets: EdgeInsets = .zero, axis: Axis? = nil, alignment: Alignment = .fill, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { _pin(to: item2, insets: insets, axis: axis, alignment: alignment, isCenteringEnabled: true, priority: priority) } @usableFromInline func _pin(to item2: LayoutItem?, insets: EdgeInsets, axis: Axis?, alignment: Alignment, isCenteringEnabled: Bool, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { let item2 = item2 ?? item._requireSuperview() item._disableAutoresizingMask() let left: NSLayoutConstraint.Attribute = isAbsolute ? .left : .leading let right: NSLayoutConstraint.Attribute = isAbsolute ? .right : .trailing var constraints = [NSLayoutConstraint]() func constrain(attribute: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation, constant: CGFloat) { let constraint = NSLayoutConstraint(item: item, attribute: attribute, relatedBy: relation, toItem: item2, attribute: attribute, multiplier: 1, constant: constant) Constraints.install(constraint, priority: priority) constraints.append(constraint) } if axis == nil || axis == .horizontal { constrain(attribute: left, relation: alignment.horizontal == .fill || alignment.horizontal == .leading ? .equal : .greaterThanOrEqual, constant: insets.left) constrain(attribute: right, relation: alignment.horizontal == .fill || alignment.horizontal == .trailing ? .equal : .lessThanOrEqual, constant: -insets.right) if alignment.horizontal == .center && isCenteringEnabled { constrain(attribute: .centerX, relation: .equal, constant: 0) } } if axis == nil || axis == .vertical { constrain(attribute: .top, relation: alignment.vertical == .fill || alignment.vertical == .top ? .equal : .greaterThanOrEqual, constant: insets.top) constrain(attribute: .bottom, relation: alignment.vertical == .fill || alignment.vertical == .bottom ? .equal : .lessThanOrEqual, constant: -insets.bottom) if alignment.vertical == .center && isCenteringEnabled { constrain(attribute: .centerY, relation: .equal, constant: 0) } } return constraints } public struct Alignment: Sendable { /// The alignment along the horizontal axis. public enum Horizontal: Sendable { /// Pin both leading and trailing edges to the superview. case fill /// Center the view in the container along the horizontal axis. case center /// Pin the leading edge to the superview and prevent the view from /// overflowing the container by pinning the trailing edge using /// "less than or equal" constraint. case leading /// Pin the trailing edge to the superview and prevent the view from /// overflowing the container by pinning the leading edge using /// "less than or equal" constraint. case trailing } /// The alignment along the vertical axis. public enum Vertical: Sendable { /// Pin both top and bottom edges to the superview. case fill /// Center the view in the container along the vertical axis. case center /// Pin the top edge to the superview and prevent the view from /// overflowing the container by pinning the bottom edge using /// "less than or equal" constraint. case top /// Pin the bottom edge to the superview and prevent the view from /// overflowing the container by pinning the top edge using /// "less than or equal" constraint. case bottom } /// The alignment along the horizontal axis. public let horizontal: Horizontal /// The alignment along the vertical axis. public let vertical: Vertical /// Initializes the alignment. public init(horizontal: Horizontal, vertical: Vertical) { (self.horizontal, self.vertical) = (horizontal, vertical) } /// The edges are pinned to the matching edges of the container with the /// given edge insets. public static let fill = Alignment(horizontal: .fill, vertical: .fill) /// The view is centered in the container and the edges are pinned using /// "less than or equal" constraints making sure it doesn't overflow the container. public static let center = Alignment(horizontal: .center, vertical: .center) /// The view is pinned to the top-leading corner of the container with the /// given edge insets and the remaining edges are pinned using "less than or /// equal" constraints making sure the view doesn't overflow the container. public static let topLeading = Alignment(horizontal: .leading, vertical: .top) /// The view is pinned to the top edge with the inset while the bottom /// edge is pinned using "less than or equal" constraint making sure the view /// doesn't overflow the container. The view is also centered horizontally. public static let top = Alignment(horizontal: .center, vertical: .top) /// The view is pinned to the top-trailing corner of the container with the /// given edge insets and the remaining edges are pinned using "less than or /// equal" constraints making sure the view doesn't overflow the container. public static let topTrailing = Alignment(horizontal: .trailing, vertical: .top) /// The view is pinned to the trailing edge with the inset while the leading /// edge is pinned using "less than or equal" constraint making sure the view /// doesn't overflow the container. The view is also centered vertically. public static let trailing = Alignment(horizontal: .trailing, vertical: .center) /// The view is pinned to the bottom-trailing corner of the container with the /// given edge insets and the remaining edges are pinned using "less than or /// equal" constraints making sure the view doesn't overflow the container. public static let bottomTrailing = Alignment(horizontal: .trailing, vertical: .bottom) /// The view is pinned to the bottom edge with the inset while the top /// edge is pinned using "less than or equal" constraint making sure the view /// doesn't overflow the container. The view is also centered horizontally. public static let bottom = Alignment(horizontal: .center, vertical: .bottom) /// The view is pinned to the bottom-leading corner of the container with the /// given edge insets and the remaining edges are pinned using "less than or /// equal" constraints making sure the view doesn't overflow the container. public static let bottomLeading = Alignment(horizontal: .leading, vertical: .bottom) /// The view is pinned to the leading edge with the inset while the trailing /// edge is pinned using "less than or equal" constraint making sure the view /// doesn't overflow the container. The view is also centered vertically. public static let leading = Alignment(horizontal: .leading, vertical: .center) } } ================================================ FILE: Sources/AnchorCollectionSize.swift ================================================ ///// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// Create multiple constraints at once by using both `width` and `height` anchors. @MainActor public struct AnchorCollectionSize { @usableFromInline let width: Anchor @usableFromInline let height: Anchor @usableFromInline init(width: Anchor, height: Anchor) { self.width = width self.height = height } // MARK: Core API /// Set the size of item. @inlinable @discardableResult public func equal(_ size: CGSize, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.equal(size.width, priority: priority), height.equal(size.height, priority: priority)] } /// Set the size of item. @inlinable @discardableResult public func greaterThanOrEqual(_ size: CGSize, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.greaterThanOrEqual(size.width, priority: priority), height.greaterThanOrEqual(size.height, priority: priority)] } /// Set the size of item. @inlinable @discardableResult public func lessThanOrEqual(_ size: CGSize, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.lessThanOrEqual(size.width, priority: priority), height.lessThanOrEqual(size.height, priority: priority)] } /// Makes the size of the item equal to the size of the other item. @inlinable @discardableResult public func equal(_ item: Item, insets: CGSize = .zero, multiplier: CGFloat = 1, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.equal(item.anchors.width * multiplier - insets.width, priority: priority), height.equal(item.anchors.height * multiplier - insets.height, priority: priority)] } @inlinable @discardableResult public func greaterThanOrEqual(_ item: Item, insets: CGSize = .zero, multiplier: CGFloat = 1, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.greaterThanOrEqual(item.anchors.width * multiplier - insets.width, priority: priority), height.greaterThanOrEqual(item.anchors.height * multiplier - insets.height, priority: priority)] } @inlinable @discardableResult public func lessThanOrEqual(_ item: Item, insets: CGSize = .zero, multiplier: CGFloat = 1, priority: LayoutPriority? = nil) -> [NSLayoutConstraint] { [width.lessThanOrEqual(item.anchors.width * multiplier - insets.width, priority: priority), height.lessThanOrEqual(item.anchors.height * multiplier - insets.height, priority: priority)] } } ================================================ FILE: Sources/Constraints.swift ================================================ /////// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// Allows you to access the underlying constraints. /// /// By default, Align automatically activates created constraints. Using /// ``Constraints`` API, constraints are activated all of the same time when you /// exit from the closure. It gives you a chance to change the `priority` of /// the created constraints. /// /// ```swift /// Constraints(for: title, subtitle) { title, subtitle in /// // Align one anchor with another /// subtitle.top.spacing(10, to: title.bottom + 10) /// /// // Manipulate dimensions /// title.width.equal(100) /// /// // Change a priority of constraints inside a group: /// subtitle.bottom.pin().priority = UILayoutPriority(999) /// } /// ``` /// /// ``Constraints`` also give you easy access to Align anchors (notice, there /// is no `.anchors` call in the example). And if you want to not activate the /// constraints, there is an option for that: /// /// ```swift /// Constraints(activate: false) { /// // Create your constraints here /// } /// ``` /// /// Nesting is supported: each constraint is collected by the innermost group, /// and each group honors its own `activate:` flag independently. public struct Constraints: RandomAccessCollection { public typealias Element = NSLayoutConstraint public typealias Index = Int public subscript(position: Int) -> NSLayoutConstraint { constraints[position] } public var startIndex: Int { constraints.startIndex } public var endIndex: Int { constraints.endIndex } public func index(after i: Int) -> Int { i + 1 } /// Returns all of the created constraints. public private(set) var constraints: [NSLayoutConstraint] /// All of the constraints created in the given closure are automatically /// activated at the same time. This is more efficient than installing them /// one-by-one. More importantly, it allows to make changes to the constraints /// before they are installed (e.g. change `priority`). /// /// - parameter activate: Set to `false` to disable automatic activation of /// constraints. /// - parameter closure: A closure in which constraints are created. The /// constraints are collected and activated together when the closure returns. @MainActor @discardableResult public init(activate: Bool = true, _ closure: () -> Void) { Constraints.stack.append([]) closure() // create constraints self.constraints = Constraints.stack.removeLast() if activate { NSLayoutConstraint.activate(constraints) } } // MARK: Activate /// Activates each constraint in the receiver. @MainActor public func activate() { NSLayoutConstraint.activate(constraints) } /// Deactivates each constraint in the receiver. @MainActor public func deactivate() { NSLayoutConstraint.deactivate(constraints) } // MARK: Adding Constraints /// Creates and automatically installs a constraint. @MainActor @usableFromInline static func add(item item1: LayoutItem, attribute attr1: NSLayoutConstraint.Attribute, relatedBy relation: NSLayoutConstraint.Relation = .equal, toItem item2: LayoutItem? = nil, attribute attr2: NSLayoutConstraint.Attribute? = nil, multiplier: CGFloat = 1, constant: CGFloat = 0, priority: LayoutPriority? = nil) -> NSLayoutConstraint { item1._disableAutoresizingMask() let constraint = NSLayoutConstraint(item: item1, attribute: attr1, relatedBy: relation, toItem: item2, attribute: attr2 ?? .notAnAttribute, multiplier: multiplier, constant: constant) install(constraint, priority: priority) return constraint } /// Creates and automatically installs a constraint between two anchors. @MainActor @usableFromInline static func add(_ lhs: consuming Anchor, _ rhs: consuming Anchor, constant: CGFloat = 0, multiplier: CGFloat = 1, relation: NSLayoutConstraint.Relation = .equal, priority: LayoutPriority? = nil) -> NSLayoutConstraint { add(item: lhs.item, attribute: lhs.attribute, relatedBy: relation, toItem: rhs.item, attribute: rhs.attribute, multiplier: (multiplier / lhs.multiplier) * rhs.multiplier, constant: constant - lhs.offset + rhs.offset, priority: priority) } /// Creates and automatically installs a constraint between an anchor and /// a given item. @MainActor @usableFromInline static func add(_ lhs: consuming Anchor, toItem item2: LayoutItem?, attribute attr2: NSLayoutConstraint.Attribute?, constant: CGFloat = 0, multiplier: CGFloat = 1, relation: NSLayoutConstraint.Relation = .equal, priority: LayoutPriority? = nil) -> NSLayoutConstraint { add(item: lhs.item, attribute: lhs.attribute, relatedBy: relation, toItem: item2, attribute: attr2, multiplier: multiplier / lhs.multiplier, constant: constant - lhs.offset, priority: priority) } @MainActor static var stack: [[NSLayoutConstraint]] = [] // this is what enables constraint auto-installing @MainActor @usableFromInline static func install(_ constraint: NSLayoutConstraint, priority: LayoutPriority? = nil) { if let priority { constraint.priority = priority } if !stack.isEmpty { stack[stack.count - 1].append(constraint) } else { constraint.isActive = true } } } extension Constraints { @MainActor @discardableResult public init(for item: repeat each Item, closure: (repeat LayoutAnchors) -> Void) { self.init { closure(repeat (each item).anchors) } } } ================================================ FILE: Sources/LayoutAnchors.swift ================================================ /// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// Provides access to the layout anchors and anchor collections. @MainActor public struct LayoutAnchors { /// The underlying item. public let item: T @inlinable public init(_ item: T) { self.item = item } // MARK: Anchors @inlinable public var top: Anchor { Anchor(item, .top) } @inlinable public var bottom: Anchor { Anchor(item, .bottom) } @inlinable public var left: Anchor { Anchor(item, .left) } @inlinable public var right: Anchor { Anchor(item, .right) } @inlinable public var leading: Anchor { Anchor(item, .leading) } @inlinable public var trailing: Anchor { Anchor(item, .trailing) } @inlinable public var centerX: Anchor { Anchor(item, .centerX) } @inlinable public var centerY: Anchor { Anchor(item, .centerY) } @inlinable public var firstBaseline: Anchor { Anchor(item, .firstBaseline) } @inlinable public var lastBaseline: Anchor { Anchor(item, .lastBaseline) } @inlinable public var width: Anchor { Anchor(item, .width) } @inlinable public var height: Anchor { Anchor(item, .height) } // MARK: Anchor Collections @inlinable public var edges: AnchorCollectionEdges { AnchorCollectionEdges(item: item) } @inlinable public var center: AnchorCollectionCenter { AnchorCollectionCenter(x: centerX, y: centerY) } @inlinable public var size: AnchorCollectionSize { AnchorCollectionSize(width: width, height: height) } } ================================================ FILE: Sources/LayoutItem.swift ================================================ /// The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif /// A type that has layout anchors: either a view or a layout guide. @MainActor public protocol LayoutItem { #if os(iOS) || os(tvOS) @_spi(Align) var superview: UIView? { get } #else @_spi(Align) var superview: NSView? { get } #endif @_spi(Align) func _disableAutoresizingMask() } extension LayoutItem { @_spi(Align) public func _disableAutoresizingMask() {} /// Wraps the SPI `superview` lookup so callers in `@inlinable` bodies don't /// have to reach for SPI directly. The body stays in the framework (this is /// `@usableFromInline`, not `@inlinable`), so the SPI access stays private. @usableFromInline func _requireSuperview() -> LayoutItem { guard let superview = superview else { preconditionFailure("Align: \(self) has no superview; pass an explicit container") } return superview } } #if os(iOS) || os(tvOS) extension UIView: LayoutItem { @_spi(Align) public func _disableAutoresizingMask() { guard translatesAutoresizingMaskIntoConstraints else { return } translatesAutoresizingMaskIntoConstraints = false } } extension UILayoutGuide: LayoutItem { @_spi(Align) public var superview: UIView? { owningView } } #elseif os(macOS) extension NSView: LayoutItem { @_spi(Align) public func _disableAutoresizingMask() { guard translatesAutoresizingMaskIntoConstraints else { return } translatesAutoresizingMaskIntoConstraints = false } } extension NSLayoutGuide: LayoutItem { @_spi(Align) public var superview: NSView? { owningView } } #endif extension LayoutItem { // Align methods are available via `LayoutAnchors` /// Provides access to the layout anchors and anchor collections. @nonobjc @inlinable public var anchors: LayoutAnchors { LayoutAnchors(self) } } #if os(iOS) || os(tvOS) public typealias EdgeInsets = UIEdgeInsets public typealias LayoutPriority = UILayoutPriority #elseif os(macOS) public typealias EdgeInsets = NSEdgeInsets public typealias LayoutPriority = NSLayoutConstraint.Priority public extension NSEdgeInsets { static let zero = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } #endif extension NSLayoutConstraint.Relation { @usableFromInline var inverted: NSLayoutConstraint.Relation { switch self { case .greaterThanOrEqual: return .lessThanOrEqual case .lessThanOrEqual: return .greaterThanOrEqual case .equal: return self @unknown default: return self } } } ================================================ FILE: Tests/AnchorAPIsTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Foundation import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorAPITests { let view = View() let container = View() init() { container.addSubview(view) } @Test func aPIs() { // Alignment view.anchors.left.equal(container.anchors.left) view.anchors.left.equal(container.anchors.left + 10) view.anchors.left.lessThanOrEqual(container.anchors.left) // Edge view.anchors.left.pin() view.anchors.left.pin(inset: 10) view.anchors.left.pin(to: container) // Center view.anchors.centerX.align() // Dimension view.anchors.width.equal(10) view.anchors.width.greaterThanOrEqual(10) view.anchors.width.equal(container.anchors.width) // AnchorCollectionEdges (Core API) view.anchors.edges.equal(container) view.anchors.edges.equal(container, insets: 20) view.anchors.edges.equal(container, insets: EdgeInsets(top: 10, left: 20, bottom: 10, right: 20)) view.anchors.edges.lessThanOrEqual(container, insets: EdgeInsets(top: 10, left: 20, bottom: 10, right: 20)) // AnchorCollectionEdges (Semantic API) view.anchors.edges.pin() view.anchors.edges.pin(insets: 20) view.anchors.edges.pin(insets: EdgeInsets(top: 10, left: 20, bottom: 10, right: 20)) view.anchors.edges.pin(to: container) view.anchors.edges.pin(axis: .horizontal) view.anchors.edges.pin(to: container, insets: 20, axis: .horizontal, alignment: .center) // AnchorCollectionCenter (Core API) view.anchors.center.equal(container) // AnchorCollectionCenter (Semantic API) view.anchors.center.align() view.anchors.center.align(with: container) // AnchorCollectionSize view.anchors.size.equal(container) view.anchors.size.greaterThanOrEqual(container) } } ================================================ FILE: Tests/AnchorAlignmentTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align /// Everything that applies for both edges and center @MainActor @Suite struct AnchorAlignmentTests { let container = View() let view = View() let a = View() let b = View() init() { container.addSubview(view) container.addSubview(a) container.addSubview(b) } // MARK: Alignments @Test func topAlignWith() { test("align top with the same edge") { let c = view.anchors.top.equal(container.anchors.top) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top)) } test("align top with the other edge") { let c = view.anchors.top.equal(container.anchors.bottom) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .bottom)) } test("align top with the center") { let c = view.anchors.top.equal(container.anchors.centerY) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .centerY)) } test("align top with offset, relation, multiplier") { let c = view.anchors.top.greaterThanOrEqual(container.anchors.top * 2 + 10) expectEqualConstraints(c, NSLayoutConstraint( item: view, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: container, attribute: .top, multiplier: 2, constant: 10 )) } } @Test func alignDifferentAnchors() { test("align bottom") { let c = view.anchors.bottom.equal(container.anchors.bottom) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom)) } test("align leading") { let c = view.anchors.leading.equal(container.anchors.leading) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .leading, toItem: container, attribute: .leading)) } test("align trailing") { let c = view.anchors.trailing.equal(container.anchors.trailing) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .trailing, toItem: container, attribute: .trailing)) } test("align left with left") { let c = view.anchors.left.equal(container.anchors.left) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .left, toItem: container, attribute: .left)) } test("align right with left") { let c = view.anchors.right.equal(container.anchors.left) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .right, toItem: container, attribute: .left)) } test("align firstBaseline with firstBaseline") { expectEqualConstraints( view.anchors.firstBaseline.equal(container.anchors.firstBaseline), NSLayoutConstraint(item: view, attribute: .firstBaseline, toItem: container, attribute: .firstBaseline) ) } test("align lastBaseline with top") { expectEqualConstraints( view.anchors.lastBaseline.equal(container.anchors.top), NSLayoutConstraint(item: view, attribute: .lastBaseline, toItem: container, attribute: .top) ) } } @Test func spacing() { test("bottom to top") { expectEqualConstraints( a.anchors.bottom.spacing(10, to: b.anchors.top), NSLayoutConstraint(item: a, attribute: .bottom, toItem: b, attribute: .top, constant: -10) ) expectEqualConstraints( a.anchors.bottom.spacing(10, to: b.anchors.top, relation: .greaterThanOrEqual), NSLayoutConstraint(item: a, attribute: .bottom, relation: .lessThanOrEqual, toItem: b, attribute: .top, constant: -10) ) expectEqualConstraints( b.anchors.top.spacing(10, to: a.anchors.bottom), NSLayoutConstraint(item: b, attribute: .top, toItem: a, attribute: .bottom, constant: 10) ) expectEqualConstraints( b.anchors.top.spacing(10, to: a.anchors.bottom, relation: .greaterThanOrEqual), NSLayoutConstraint(item: b, attribute: .top, relation: .greaterThanOrEqual, toItem: a, attribute: .bottom, constant: 10) ) } test("top to top") { expectEqualConstraints( a.anchors.top.spacing(10, to: b.anchors.top), NSLayoutConstraint(item: a, attribute: .top, toItem: b, attribute: .top, constant: 10) ) expectEqualConstraints( a.anchors.top.spacing(10, to: b.anchors.top, relation: .greaterThanOrEqual), NSLayoutConstraint(item: a, attribute: .top, relation: .greaterThanOrEqual, toItem: b, attribute: .top, constant: 10) ) expectEqualConstraints( b.anchors.top.spacing(10, to: a.anchors.top), NSLayoutConstraint(item: b, attribute: .top, toItem: a, attribute: .top, constant: 10) ) expectEqualConstraints( b.anchors.top.spacing(10, to: a.anchors.top, relation: .greaterThanOrEqual), NSLayoutConstraint(item: b, attribute: .top, relation: .greaterThanOrEqual, toItem: a, attribute: .top, constant: 10) ) } // [a] [b] test("right to left") { expectEqualConstraints( a.anchors.right.spacing(10, to: b.anchors.left), NSLayoutConstraint(item: a, attribute: .right, toItem: b, attribute: .left, constant: -10) ) expectEqualConstraints( a.anchors.right.spacing(10, to: b.anchors.left, relation: .greaterThanOrEqual), NSLayoutConstraint(item: a, attribute: .right, relation: .lessThanOrEqual, toItem: b, attribute: .left, constant: -10) ) expectEqualConstraints( a.anchors.right.spacing(10, to: b.anchors.left, relation: .lessThanOrEqual), NSLayoutConstraint(item: a, attribute: .right, relation: .greaterThanOrEqual, toItem: b, attribute: .left, constant: -10) ) expectEqualConstraints( b.anchors.left.spacing(10, to: a.anchors.right), NSLayoutConstraint(item: b, attribute: .left, toItem: a, attribute: .right, constant: 10) ) expectEqualConstraints( b.anchors.left.spacing(10, to: a.anchors.right, relation: .greaterThanOrEqual), NSLayoutConstraint(item: b, attribute: .left, relation: .greaterThanOrEqual, toItem: a, attribute: .right, constant: 10) ) expectEqualConstraints( b.anchors.left.spacing(10, to: a.anchors.right, relation: .lessThanOrEqual), NSLayoutConstraint(item: b, attribute: .left, relation: .lessThanOrEqual, toItem: a, attribute: .right, constant: 10) ) } } } ================================================ FILE: Tests/AnchorCenterTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorCenterTests { let container = View() let view = View() init() { container.addSubview(view) } // MARK: Align With Superview @Test func alignWithSuperview() { expectEqualConstraints( view.anchors.centerX.align(), NSLayoutConstraint(item: view, attribute: .centerX, toItem: container, attribute: .centerX) ) expectEqualConstraints( view.anchors.centerY.align(), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY) ) expectEqualConstraints( view.anchors.centerY.align(offset: -10), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY, constant: -10) ) } } ================================================ FILE: Tests/AnchorCollectionCenterTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorCollectionCenterTests { let container = View() let view = View() init() { container.addSubview(view) } @Test func core() { expectEqualConstraints( view.anchors.center.equal(container), [NSLayoutConstraint(item: view, attribute: .centerX, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY)] ) expectEqualConstraints( view.anchors.center.equal(container, offset: CGPoint(x: 10, y: 20)), [NSLayoutConstraint(item: view, attribute: .centerX, toItem: container, attribute: .centerX, constant: 10), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY, constant: 20)] ) expectEqualConstraints( view.anchors.center.greaterThanOrEqual(container), [NSLayoutConstraint(item: view, attribute: .centerX, relation: .greaterThanOrEqual, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, relation: .greaterThanOrEqual, toItem: container, attribute: .centerY)] ) expectEqualConstraints( view.anchors.center.lessThanOrEqual(container), [NSLayoutConstraint(item: view, attribute: .centerX, relation: .lessThanOrEqual, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, relation: .lessThanOrEqual, toItem: container, attribute: .centerY)] ) } @Test func align() { expectEqualConstraints( view.anchors.center.align(with: container), [NSLayoutConstraint(item: view, attribute: .centerX, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY)] ) } @Test func alignWithSuperview() { expectEqualConstraints( view.anchors.center.align(), [NSLayoutConstraint(item: view, attribute: .centerX, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, toItem: container, attribute: .centerY)] ) } } ================================================ FILE: Tests/AnchorCollectionEdgesTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorCollectionEdgesTests { let container = View() let view = View() init() { container.addSubview(view) } // MARK: - Core API @Test func equal() { expectEqualConstraints( view.anchors.edges.equal(container), [NSLayoutConstraint(item: view, attribute: .top, relation: .equal, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .leading, relation: .equal,toItem: container, attribute: .leading), NSLayoutConstraint(item: view, attribute: .bottom, relation: .equal, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .trailing, relation: .equal, toItem: container, attribute: .trailing)] ) } @Test func equalAbsolute() { expectEqualConstraints( view.anchors.edges.absolute.equal(container), [NSLayoutConstraint(item: view, attribute: .top, relation: .equal, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .left, relation: .equal,toItem: container, attribute: .left), NSLayoutConstraint(item: view, attribute: .bottom, relation: .equal, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .right, relation: .equal, toItem: container, attribute: .right)] ) } @Test func equalWithInsets() { expectEqualConstraints( view.anchors.edges.equal(container, insets: 20), [NSLayoutConstraint(item: view, attribute: .top, relation: .equal, toItem: container, attribute: .top, constant: 20), NSLayoutConstraint(item: view, attribute: .leading, relation: .equal,toItem: container, attribute: .leading, constant: 20), NSLayoutConstraint(item: view, attribute: .bottom, relation: .equal, toItem: container, attribute: .bottom, constant: -20), NSLayoutConstraint(item: view, attribute: .trailing, relation: .equal, toItem: container, attribute: .trailing, constant: -20)] ) } @Test func pinToSuperviewLessThanOrEqual() { expectEqualConstraints( view.anchors.edges.lessThanOrEqual(container), [NSLayoutConstraint(item: view, attribute: .top, relation: .greaterThanOrEqual, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .leading, relation: .greaterThanOrEqual,toItem: container, attribute: .leading), NSLayoutConstraint(item: view, attribute: .bottom, relation: .lessThanOrEqual, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .trailing, relation: .lessThanOrEqual, toItem: container, attribute: .trailing)] ) } // MARK: - Semantic API @Test func pinToSuperview() { expectEqualConstraints( view.anchors.edges.pin(), [NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .leading, toItem: container, attribute: .leading), NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .trailing, toItem: container, attribute: .trailing)] ) } @Test func pinToSuperviewAbsolute() { expectEqualConstraints( view.anchors.edges.absolute.pin(), [NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .left, toItem: container, attribute: .left), NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .right, toItem: container, attribute: .right)] ) } @Test func pinToSuperviewWithInsets() { let insets = EdgeInsets(top: 1, left: 2, bottom: 3, right: 4) expectEqualConstraints( view.anchors.edges.pin(insets: insets), [NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 1), NSLayoutConstraint(item: view, attribute: .leading, toItem: container, attribute: .leading, constant: 2), NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom, constant: -3), NSLayoutConstraint(item: view, attribute: .trailing, toItem: container, attribute: .trailing, constant: -4)] ) } @Test func pinToSuperviewLessThanOrEqualAlignmentCenter() { expectEqualConstraints( view.anchors.edges.pin(alignment: .center), [NSLayoutConstraint(item: view, attribute: .top, relation: .greaterThanOrEqual, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .leading, relation: .greaterThanOrEqual,toItem: container, attribute: .leading), NSLayoutConstraint(item: view, attribute: .bottom, relation: .lessThanOrEqual, toItem: container, attribute: .bottom), NSLayoutConstraint(item: view, attribute: .trailing, relation: .lessThanOrEqual, toItem: container, attribute: .trailing), NSLayoutConstraint(item: view, attribute: .centerX, relation: .equal, toItem: container, attribute: .centerX), NSLayoutConstraint(item: view, attribute: .centerY, relation: .equal, toItem: container, attribute: .centerY)] ) } @Test func pinToSuperviewAlongAxis() { expectEqualConstraints( view.anchors.edges.pin(axis: .vertical), [NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top), NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom)] ) expectEqualConstraints( view.anchors.edges.pin(axis: .horizontal), [NSLayoutConstraint(item: view, attribute: .leading, toItem: container, attribute: .leading), NSLayoutConstraint(item: view, attribute: .trailing, toItem: container, attribute: .trailing)] ) } } ================================================ FILE: Tests/AnchorCollectionSizeTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorCollectionSizeTests { let container = View() let view = View() init() { container.addSubview(view) } @Test func setSize() { expectEqualConstraints( view.anchors.size.equal(CGSize(width: 5, height: 10)), [NSLayoutConstraint(item: view, attribute: .width, constant: 5), NSLayoutConstraint(item: view, attribute: .height, constant: 10)] ) } @Test func matchSize() { expectEqualConstraints( view.anchors.size.equal(container), [NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .height)] ) } @Test func matchSizeWithInsets() { expectEqualConstraints( view.anchors.size.equal(container, insets: CGSize(width: 10, height: 20)), [NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, constant: -10), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .height, constant: -20)] ) } @Test func matchSizeMultiplier() { expectEqualConstraints( view.anchors.size.equal(container, multiplier: 2), [NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 2), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .height, multiplier: 2)] ) } } ================================================ FILE: Tests/AnchorDimensionTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorDimensionTests { let container = View() let view = View() init() { container.addSubview(view) } @Test func setWidth() { expectEqualConstraints( view.anchors.width.equal(10), NSLayoutConstraint(item: view, attribute: .width, constant: 10) ) expectEqualConstraints( view.anchors.width.greaterThanOrEqual(10), NSLayoutConstraint(item: view, attribute: .width, relation: .greaterThanOrEqual, constant: 10) ) } @Test func setHeight() { expectEqualConstraints( view.anchors.height.equal(10), NSLayoutConstraint(item: view, attribute: .height, constant: 10) ) expectEqualConstraints( view.anchors.height.greaterThanOrEqual(10), NSLayoutConstraint(item: view, attribute: .height, relation: .greaterThanOrEqual, constant: 10) ) } @Test func matchWidth() { expectEqualConstraints( view.anchors.width.equal(container.anchors.width), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width) ) expectEqualConstraints( view.anchors.width.equal(container.anchors.height), // can mix and match NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .height) ) } @Test func clamp() { expectEqualConstraints( view.anchors.height.clamp(to: 10...20), [NSLayoutConstraint(item: view, attribute: .height, relation: .greaterThanOrEqual, constant: 10), NSLayoutConstraint(item: view, attribute: .height, relation: .lessThanOrEqual, constant: 20)] ) } } ================================================ FILE: Tests/AnchorEdgeTests.swift ================================================ // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorEdgeTests { let container = View() let view = View() init() { container.addSubview(view) } // MARK: Pinning @Test func pinToSuperview() { test("pin top to superview") { let c = view.anchors.top.pin() expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top)) } test("pin top to superview with inset") { let c = view.anchors.top.pin(inset: 10) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 10)) } test("pin bottom to superview with inset") { let c = view.anchors.bottom.pin(inset: 10) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom, constant: -10)) } test("pin left to superview with inset") { let c = view.anchors.left.pin(inset: 10) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .left, toItem: container, attribute: .left, constant: 10)) } test("pin right to superview with inset") { let c = view.anchors.right.pin(inset: 10) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .right, toItem: container, attribute: .right, constant: -10)) } } #if os(iOS) || os(tvOS) @Test func pinToSuperviewMargin() { test("pin top to superview margin") { let c = view.anchors.top.pin(to: container.layoutMarginsGuide) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container.layoutMarginsGuide, attribute: .top)) } test("pin top to superview margin with inset") { let c = view.anchors.top.pin(to: container.layoutMarginsGuide, inset: 10) expectEqualConstraints(c, NSLayoutConstraint(item: view, attribute: .top, toItem: container.layoutMarginsGuide, attribute: .top, constant: 10)) } } #endif } ================================================ FILE: Tests/AnchorPerformanceTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align // WARNING: Don't forget to compile for Release mode! @MainActor @Suite struct AnchorPerformanceTests { @available(iOS 16, tvOS 16, macOS 13, watchOS 9, *) @Test func pin() { let view = View() let container = View() container.addSubview(view) let clock = ContinuousClock() let elapsed = clock.measure { for _ in 0...10_000 { view.anchors.edges.pin(alignment: .center) } } print("testPin: \(format(elapsed))") } @available(iOS 16, tvOS 16, macOS 13, watchOS 9, *) @Test func batchedPin() { let view = View() let container = View() container.addSubview(view) let clock = ContinuousClock() let elapsed = clock.measure { for _ in 0...10_000 { Constraints { view.anchors.edges.pin(alignment: .center) } } } print("batchedPin: \(format(elapsed))") } @available(iOS 16, tvOS 16, macOS 13, watchOS 9, *) @Test func constraintsPin() { let view = View() let container = View() container.addSubview(view) Constraints(for: view, container) { view, container in view.top.equal(container.top) } let clock = ContinuousClock() let elapsed = clock.measure { Constraints { for _ in 0...10_000 { view.anchors.width.equal(view.anchors.height) } } } print("testConstraintsPin: \(format(elapsed))") } @available(iOS 16, tvOS 16, macOS 13, watchOS 9, *) private func format(_ duration: Duration) -> String { let ms = Double(duration.components.seconds) * 1_000 + Double(duration.components.attoseconds) / 1e15 return String(format: "%.3f ms", ms) } } ================================================ FILE: Tests/AnchorTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct AnchorTests { let container = View() let view = View() init() { container.addSubview(view) } @Test func constraintsCreatedByAnchorsAreInstalledAutomatically() { let constraint = view.anchors.top.equal(container.anchors.top) #expect(constraint.isActive == true) } @Test func firstViewSetToTranslatesAutoresizingMaskIntoConstraints() { // make sure that the precondition is always true view.translatesAutoresizingMaskIntoConstraints = true container.translatesAutoresizingMaskIntoConstraints = true #expect(view.translatesAutoresizingMaskIntoConstraints) #expect(container.translatesAutoresizingMaskIntoConstraints) view.anchors.top.equal(container.anchors.top) #expect(!view.translatesAutoresizingMaskIntoConstraints) #expect(container.translatesAutoresizingMaskIntoConstraints) view.translatesAutoresizingMaskIntoConstraints = true container.anchors.top.equal(view.anchors.top) #expect(view.translatesAutoresizingMaskIntoConstraints) #expect(!container.translatesAutoresizingMaskIntoConstraints) } // MARK: Offsetting @Test func offsettingTopAnchor() { let anchor = container.anchors.top + 10 expectEqualConstraints( view.anchors.top.equal(anchor), NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 10) ) expectEqualConstraints( view.anchors.top.equal(anchor + 10), NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 20) ) expectEqualConstraints( // test that works both ways anchor.equal(view.anchors.top), NSLayoutConstraint(item: container, attribute: .top, toItem: view, attribute: .top, constant: -10) ) } @Test func offsettingUsingOperators() { expectEqualConstraints( view.anchors.top.equal(container.anchors.top + 10), NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 10) ) expectEqualConstraints( view.anchors.top.equal(container.anchors.top - 10), NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: -10) ) } @Test func offsettingRightAnchor() { let anchor = container.anchors.right - 10 expectEqualConstraints( view.anchors.right.equal(anchor), NSLayoutConstraint(item: view, attribute: .right, toItem: container, attribute: .right, constant: -10) ) expectEqualConstraints( // test that works both ways anchor.equal(view.anchors.right), NSLayoutConstraint(item: container, attribute: .right, toItem: view, attribute: .right, constant: 10) ) } @Test func aligningTwoOffsetAnchors() { let containerTop = container.anchors.top + 10 let viewTop = view.anchors.top + 10 expectEqualConstraints( viewTop.equal(containerTop), // nobody's going to do that, but it's nice it's their NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top, constant: 0) ) } @Test func offsettingWidth() { expectEqualConstraints( view.anchors.height.equal(container.anchors.width + 10), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .width, constant: 10) ) expectEqualConstraints( view.anchors.height.equal(container.anchors.width - 10), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .width, constant: -10) ) } @Test func offsetingMultipleTimes() { expectEqualConstraints( view.anchors.height.equal((container.anchors.width + 10) + 10), NSLayoutConstraint(item: view, attribute: .height, toItem: container, attribute: .width, constant: 20) ) } // MARK: Multiplying @Test func multiplyingWidth() { expectEqualConstraints( view.anchors.width.equal(container.anchors.width * 0.5), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5) ) expectEqualConstraints( (view.anchors.width * 2).equal(container.anchors.width), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5) ) } @Test func multiplyingMultipleTimes() { expectEqualConstraints( view.anchors.width.equal((container.anchors.width * 0.5) * 0.5), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.25) ) expectEqualConstraints( ((view.anchors.width * 2) * 2).equal(container.anchors.width), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.25) ) } @Test func multiplyingBothAnchors() { expectEqualConstraints( (view.anchors.width * 0.5).equal(container.anchors.width * 0.5), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 1) ) } // MARK: Dividing @Test func dividingWidth() { expectEqualConstraints( view.anchors.width.equal(container.anchors.width / 2), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5) ) expectEqualConstraints( (view.anchors.width / 2).equal(container.anchors.width), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 2) ) } @Test func dividingMatchesMultiplyingByReciprocal() { expectEqualConstraints( view.anchors.width.equal(container.anchors.width / 4), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.25) ) } // MARK: Mixing Multiplier and Offset @Test func mixingMultiplierAndOffset() { expectEqualConstraints( view.anchors.width.equal(container.anchors.width * 0.5 + 10), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5, constant: 10) ) expectEqualConstraints( view.anchors.width.equal((container.anchors.width + 10) * 0.5), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5, constant: 5) ) expectEqualConstraints( view.anchors.width.equal((container.anchors.width + 10) * 0.5 + 7), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 0.5, constant: 12) ) expectEqualConstraints( view.anchors.width.equal(((container.anchors.width + 10) * 0.5 + 7) * 2), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 1, constant: 24) ) expectEqualConstraints( view.anchors.width.equal(container.anchors.width + 10 * 0.5), NSLayoutConstraint(item: view, attribute: .width, toItem: container, attribute: .width, multiplier: 1, constant: 5) ) } } ================================================ FILE: Tests/ConstraintsTests.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif import Align @MainActor @Suite struct ConstraintsTests { let container = View() let view = View() init() { container.addSubview(view) } @Test func canChangePriorityInsideInit() { Constraints { let c = view.anchors.top.pin() #expect(c.priority.rawValue == 1000) c.priority = LayoutPriority(999) #expect(c.priority.rawValue == 999) } } // MARK: Nesting @Test func callsCanBeNested() { // no arguments Constraints() { expectEqualConstraints( view.anchors.top.pin(), NSLayoutConstraint(item: view, attribute: .top, toItem: container, attribute: .top) ) Constraints() { expectEqualConstraints( view.anchors.bottom.pin(), NSLayoutConstraint(item: view, attribute: .bottom, toItem: container, attribute: .bottom) ) } } } } @MainActor @Suite struct ConstraintsArityTests { let container = View() let a = View() let b = View() let c = View() let d = View() init() { container.addSubview(a) container.addSubview(b) container.addSubview(c) container.addSubview(d) } // MARK: Test That Behaves Like Standard Init @Test func arity() { Constraints(for: a, b) { a, b in let constraint = a.top.equal(b.top) expectEqualConstraints(constraint, NSLayoutConstraint( item: self.a, attribute: .top, relation: .equal, toItem: self.b, attribute: .top )) } } @Test func canChangePriorityInsideInit() { Constraints(for: a) { let cons = $0.top.pin() #expect(cons.priority.rawValue == 1000) cons.priority = LayoutPriority(999) #expect(cons.priority.rawValue == 999) } } @Test func callsCanBeNested() { Constraints(for: a) { expectEqualConstraints( $0.top.pin(), NSLayoutConstraint(item: a, attribute: .top, toItem: container, attribute: .top) ) Constraints(for: a) { expectEqualConstraints( $0.bottom.pin(), NSLayoutConstraint(item: a, attribute: .bottom, toItem: container, attribute: .bottom) ) } } } @Test func collection() { let constraints = Constraints { a.anchors.top.pin() a.anchors.bottom.pin() } expectEqualConstraints(constraints.constraints, [ NSLayoutConstraint(item: a, attribute: .top, toItem: container, attribute: .top), NSLayoutConstraint(item: a, attribute: .bottom, toItem: container, attribute: .bottom) ]) expectEqualConstraints( constraints.constraints[0], NSLayoutConstraint(item: a, attribute: .top, toItem: container, attribute: .top) ) expectEqualConstraints( constraints.constraints[1], NSLayoutConstraint(item: a, attribute: .bottom, toItem: container, attribute: .bottom) ) } // MARK: Multiple Arguments @Test func one() { Constraints(for: a) { #expect($0.item === a) return } } @Test func two() { Constraints(for: a, b) { #expect($0.item === a) #expect($1.item === b) } } @Test func three() { Constraints(for: a, b, c) { #expect($0.item === a) #expect($1.item === b) #expect($2.item === c) } } @Test func four() { Constraints(for: a, b, c, d) { #expect($0.item === a) #expect($1.item === b) #expect($2.item === c) #expect($3.item === d) } } } #if os(iOS) || os(tvOS) @MainActor @Suite struct AddingSubviewsTests { let container = View() let a = View() let b = View() let c = View() let d = View() @Test func one() { container.addSubview(a) { #expect($0.item.superview === container) #expect($0.item === a) return } } @Test func two() { container.addSubview(a, b) { #expect($0.item.superview === container) #expect($1.item.superview === container) #expect($0.item === a) #expect($1.item === b) } } @Test func three() { container.addSubview(a, b, c) { #expect($0.item.superview === container) #expect($1.item.superview === container) #expect($2.item.superview === container) #expect($0.item === a) #expect($1.item === b) #expect($2.item === c) } } @Test func four() { container.addSubview(a, b, c, d) { #expect($0.item.superview === container) #expect($1.item.superview === container) #expect($2.item.superview === container) #expect($3.item.superview === container) #expect($0.item === a) #expect($1.item === b) #expect($2.item === c) #expect($3.item === d) } } } #endif ================================================ FILE: Tests/Extensions/Align+Extensions.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). #if os(iOS) || os(tvOS) import Align import UIKit // MARK: - UIView + Constraints public extension UIView { @discardableResult @nonobjc func addSubview( _ item: repeat each Item, constraints: (repeat LayoutAnchors) -> Void ) -> Constraints { _ = (repeat addSubview(each item)) return Constraints(for: repeat each item, closure: constraints) } } #endif ================================================ FILE: Tests/Extensions/Diff.swift ================================================ // // Difference.swift // Difference // // Created by Krzysztof Zablocki on 18.10.2017 // Copyright © 2017 Krzysztof Zablocki. All rights reserved. // import Foundation fileprivate extension String { init(dumping object: T) { self.init() dump(object, to: &self) } } /// Compares 2 objects and iterates over their differences /// /// - Parameters: /// - lhs: expected object /// - rhs: received object /// - closure: iteration closure fileprivate func diff(_ expected: T, _ received: T, level: Int = 0, closure: (_ description: String) -> Void) { let lhsMirror = Mirror(reflecting: expected) let rhsMirror = Mirror(reflecting: received) guard lhsMirror.children.count != 0 else { if String(dumping: received) != String(dumping: expected) { closure("received: \"\(received)\" expected: \"\(expected)\"\n") } return } let zipped = zip(lhsMirror.children, rhsMirror.children) zipped.forEach { (lhs, rhs) in let leftDump = String(dumping: lhs.value) if leftDump != String(dumping: rhs.value) { if let notPrimitive = Mirror(reflecting: lhs.value).displayStyle, notPrimitive != .tuple { var results = [String]() diff(lhs.value, rhs.value, level: level + 1) { diff in results.append(diff) } if !results.isEmpty { closure("child \(lhs.label ?? ""):\n\((0..(_ expected: T, _ received: T) -> [String] { var all = [String]() diff(expected, received) { all.append($0) } return all } /// Prints list of differences between 2 objects /// /// - Parameters: /// - expected: Expected value /// - received: Received value public func dumpDiff(_ expected: T, _ received: T) { // skip equal guard expected != received else { return } diff(expected, received).forEach { print($0) } } /// Prints list of differences between 2 objects /// /// - Parameters: /// - expected: Expected value /// - received: Received value public func dumpDiff(_ expected: T, _ received: T) { diff(expected, received).forEach { print($0) } } ================================================ FILE: Tests/Extensions/XCTestExtensions.swift ================================================ // The MIT License (MIT) // // Copyright (c) 2017-2026 Alexander Grebenyuk (github.com/kean). import Testing #if os(iOS) || os(tvOS) import UIKit #elseif os(macOS) import AppKit #endif func test(_ title: String? = nil, _ closure: () -> Void) { closure() } func test(_ title: String? = nil, with element: T, _ closure: (T) -> Void) { closure(element) } // MARK: Asserts @MainActor public func expectEqualConstraints(_ expected: [NSLayoutConstraint], _ received: [NSLayoutConstraint], sourceLocation: SourceLocation = #_sourceLocation) { #expect(expected.count == received.count, sourceLocation: sourceLocation) var received = received for c in expected { let idx = received.firstIndex(where: { Constraint($0) == Constraint(c) }) #expect(idx != nil, "Failed to find constraints: \(c)\n\nExpected: \(expected)\n\nReceived: \(received)", sourceLocation: sourceLocation) if let idx = idx { received.remove(at: idx) } } } @MainActor public func expectEqualConstraints(_ expected: NSLayoutConstraint, _ received: NSLayoutConstraint, sourceLocation: SourceLocation = #_sourceLocation) { expectEqualConstraints(Constraint(expected), Constraint(received), sourceLocation: sourceLocation) } @MainActor private func expectEqualConstraints(_ expected: T, _ received: T, sourceLocation: SourceLocation = #_sourceLocation) { print(diff(expected, received)) #expect(expected == received, "Found difference for \(diff(expected, received).joined(separator: ", "))", sourceLocation: sourceLocation) } // MARK: Constraints extension NSLayoutConstraint { @nonobjc convenience init(item item1: Any, attribute attr1: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation = .equal, toItem item2: Any? = nil, attribute attr2: NSLayoutConstraint.Attribute? = nil, multiplier: CGFloat = 1, constant: CGFloat = 0, priority: Float? = nil, id: String? = nil) { self.init(item: item1, attribute: attr1, relatedBy: relation, toItem: item2, attribute: attr2 ?? .notAnAttribute, multiplier: multiplier, constant: constant) if let priority = priority { self.priority = LayoutPriority(priority) } if let id = id { self.identifier = id } } } @MainActor private struct Constraint { let firstItem: AnyObject? let firstAttribute: String let secondItem: AnyObject? let secondAttribute: String let relation: NSLayoutConstraint.Relation.RawValue let multiplier: CGFloat let constant: CGFloat let priority: LayoutPriority.RawValue let identifier: String? init(_ c: NSLayoutConstraint) { firstItem = c.firstItem firstAttribute = c.firstAttribute.toString secondItem = c.secondItem secondAttribute = c.secondAttribute.toString relation = c.relation.rawValue multiplier = c.multiplier constant = c.constant priority = c.priority.rawValue identifier = c.identifier } } #if swift(>=6.0) extension Constraint: @preconcurrency Equatable { static func ==(lhs: Constraint, rhs: Constraint) -> Bool { return lhs.firstItem === rhs.firstItem && lhs.firstAttribute == rhs.firstAttribute && lhs.relation == rhs.relation && lhs.secondItem === rhs.secondItem && lhs.secondAttribute == rhs.secondAttribute && lhs.multiplier == rhs.multiplier && lhs.constant == rhs.constant && lhs.priority == rhs.priority && lhs.identifier == rhs.identifier } } #else extension Constraint: Equatable { static func ==(lhs: Constraint, rhs: Constraint) -> Bool { return lhs.firstItem === rhs.firstItem && lhs.firstAttribute == rhs.firstAttribute && lhs.relation == rhs.relation && lhs.secondItem === rhs.secondItem && lhs.secondAttribute == rhs.secondAttribute && lhs.multiplier == rhs.multiplier && lhs.constant == rhs.constant && lhs.priority == rhs.priority && lhs.identifier == rhs.identifier } } #endif // MARK: Helpers extension NSLayoutConstraint.Attribute { var toString: String { switch self { case .width: return "width" case .height: return "height" case .bottom: return "bottom" case .top: return "top" case .left: return "left" case .right: return "right" case .leading: return "leading" case .trailing: return "trailing" case .centerX: return "centerX" case .centerY: return "centerY" case .lastBaseline: return "lastBaseline" case .firstBaseline: return "firstBaseline" case .notAnAttribute: return "notAnAttribute" #if os(iOS) || os(tvOS) case .bottomMargin: return "bottomMargin" case .topMargin: return "topMargin" case .leftMargin: return "leftMargin" case .rightMargin: return "rightMargin" case .leadingMargin: return "leadingMargin" case .trailingMargin: return "trailingMargin" case .centerXWithinMargins: return "centerXWithinMargins" case .centerYWithinMargins: return "centerYWithinMargins" #endif @unknown default: return "unexpected" } } } #if os(iOS) || os(tvOS) typealias View = UIView typealias LayoutPriority = UILayoutPriority #elseif os(macOS) typealias View = NSView typealias LayoutPriority = NSLayoutConstraint.Priority #endif