{"id":22085573,"url":"https://github.com/tejasmr/autolayoutconstraints","last_synced_at":"2025-03-23T22:15:58.486Z","repository":{"id":144525874,"uuid":"342555011","full_name":"tejasmr/AutoLayoutConstraints","owner":"tejasmr","description":"Library for auto layout constraints programmatically for UIKit views","archived":false,"fork":false,"pushed_at":"2021-02-26T12:04:48.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-29T05:24:23.265Z","etag":null,"topics":["auto-layout","auto-layout-programmatically","ios","swift","uikit"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tejasmr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-26T11:28:15.000Z","updated_at":"2022-05-15T07:36:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"31864c9c-f2af-40f5-984d-947efa6c4fbe","html_url":"https://github.com/tejasmr/AutoLayoutConstraints","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tejasmr%2FAutoLayoutConstraints","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tejasmr%2FAutoLayoutConstraints/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tejasmr%2FAutoLayoutConstraints/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tejasmr%2FAutoLayoutConstraints/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tejasmr","download_url":"https://codeload.github.com/tejasmr/AutoLayoutConstraints/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245175480,"owners_count":20572787,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-layout","auto-layout-programmatically","ios","swift","uikit"],"created_at":"2024-12-01T01:15:04.663Z","updated_at":"2025-03-23T22:15:58.459Z","avatar_url":"https://github.com/tejasmr.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AutoLayoutConstraints\n\n## CODE\n\n```swift\nimport Foundation\nimport SwiftUI\nimport UIKit\n\nstruct AutoLayoutConstraints {\n    // subView is view for which you want to add the ALC\n    var subView: UIView\n    // width constraint - in points or relative to the screenWidth = UIScreen.main.bounds.width\n    var width: CGFloat\n    // height constraint - in points or relative to the screenHeight = UIScreen.main.bounds.height\n    var height: CGFloat\n    // xOffset is the offset from either the left margin or right margin\n    var xOffset: CGFloat = 0.0\n    // yOffset is the offset from either the top margin or bottom margin\n    var yOffset: CGFloat = 0.0\n    // whether the subView should be centered horizontally\n    var xCentered: Bool = true\n    // whether the subview should be centered verically\n    var yCentered: Bool = true\n    // if this is true, the we offset it from the leading margin, else we do it from trailing margin\n    var xLeading: Bool = true\n    // if this is true, the we offset it from the top margin, else we do it from bottom margin\n    var yTop: Bool = true\n    \n    func setAutoConstraints(_ viewController: UIViewController) {\n        \n        // I don't know what this does.\n        subView.translatesAutoresizingMaskIntoConstraints = false\n        \n        let widthConstraint = NSLayoutConstraint(\n           item: subView,\n           attribute: NSLayoutConstraint.Attribute.width,\n           relatedBy: NSLayoutConstraint.Relation.equal,\n           toItem: nil,\n           attribute: NSLayoutConstraint.Attribute.notAnAttribute,\n           multiplier: 1,\n            constant: width\n        )\n        let heightConstraint = NSLayoutConstraint(\n           item: subView,\n           attribute: NSLayoutConstraint.Attribute.height,\n           relatedBy: NSLayoutConstraint.Relation.equal,\n           toItem: nil,\n           attribute: NSLayoutConstraint.Attribute.notAnAttribute,\n           multiplier: 1,\n            constant: height\n        )\n        var xConstraint = NSLayoutConstraint(\n            item: subView,\n            attribute: NSLayoutConstraint.Attribute.centerX,\n            relatedBy: NSLayoutConstraint.Relation.equal,\n            toItem: viewController.view,\n            attribute: NSLayoutConstraint.Attribute.centerX,\n            multiplier: 1,\n            constant: 0\n        )\n        var yConstraint = NSLayoutConstraint(\n            item: subView,\n            attribute: NSLayoutConstraint.Attribute.centerY,\n            relatedBy: NSLayoutConstraint.Relation.equal,\n            toItem: viewController.view,\n            attribute: NSLayoutConstraint.Attribute.centerY,\n            multiplier: 1,\n            constant: 0\n        )\n        if !xCentered {\n            if xLeading {\n                xConstraint = NSLayoutConstraint(\n                   item: subView,\n                   attribute: NSLayoutConstraint.Attribute.leading,\n                   relatedBy: NSLayoutConstraint.Relation.equal,\n                   toItem: viewController.view,\n                   attribute: NSLayoutConstraint.Attribute.leadingMargin,\n                   multiplier: 1.0,\n                    constant: xOffset\n                )\n            }\n            else {\n                xConstraint = NSLayoutConstraint(\n                   item: subView,\n                   attribute: NSLayoutConstraint.Attribute.trailing,\n                   relatedBy: NSLayoutConstraint.Relation.equal,\n                   toItem: viewController.view,\n                   attribute: NSLayoutConstraint.Attribute.trailingMargin,\n                   multiplier: 1.0,\n                    constant: -xOffset\n                )\n            }\n        }\n        if !yCentered {\n            if yTop {\n                yConstraint = NSLayoutConstraint(\n                    item: subView,\n                    attribute: NSLayoutConstraint.Attribute.top,\n                    relatedBy: NSLayoutConstraint.Relation.equal,\n                    toItem: viewController.view,\n                    attribute: NSLayoutConstraint.Attribute.topMargin,\n                    multiplier: 1.0,\n                    constant: yOffset\n                )\n            }\n            else {\n                yConstraint = NSLayoutConstraint(\n                    item: subView,\n                    attribute: NSLayoutConstraint.Attribute.bottom,\n                    relatedBy: NSLayoutConstraint.Relation.equal,\n                    toItem: viewController.view,\n                    attribute: NSLayoutConstraint.Attribute.bottomMargin,\n                    multiplier: 1.0,\n                    constant: -yOffset\n                )\n            }\n        }\n        viewController.view.addConstraints(\n                [widthConstraint, heightConstraint, xConstraint, yConstraint]\n        )\n    }\n}\n```\n\n## Usage (under construction)\n\n### Some supporting variables for the following examples\n```swift\n// width of the screen\nlet screenWidth = UIScreen.main.bounds.width\nlet screenHeight = UIScreen.main.bounds.height\nlet label = UILabel()\n```\n### If you want a UI Component at the center of the screen\n```swift\n// Inside UIViewController, in some function like override func viewDidLoad()\nlet labelALC = AutoLayoutConstraints(subView: label, width: screenWidth / 2, height: screenHeight / 4\")\nlabelALC.setConstraints(self)\n```\nLet us unpack the first statement.\n\nsubView: label -\u003e sets the subView property of the AutoLayoutConstraints object as the UILabel named label\n\nwidth: screenWidth / 2 -\u003e sets the width property of the AutoLayoutConstraints object as half of the screenWidth\n\nheight: screenHeight / 4 -\u003e sets the height property of the AutoLayoutConstraints object as quater of the screenHeight\n\nIn the second statement.\n\nWe pass the UIViewController as the parameter, so the constraints will be added to the view property of the viewController.\n\nThe assumptions made here are:\n\n1. The subView is centered vertically.\n\n2. The subView is centered horizontally.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftejasmr%2Fautolayoutconstraints","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftejasmr%2Fautolayoutconstraints","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftejasmr%2Fautolayoutconstraints/lists"}