{"id":25215489,"url":"https://github.com/muizidn/flowconstraint","last_synced_at":"2025-04-05T08:43:17.465Z","repository":{"id":42443939,"uuid":"179302421","full_name":"muizidn/flowconstraint","owner":"muizidn","description":"Write constraint like a flow!","archived":false,"fork":false,"pushed_at":"2022-07-22T03:05:00.000Z","size":248,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-10T18:14:37.296Z","etag":null,"topics":["autolayout","chain","constraint","fluent","microlibrary","swift"],"latest_commit_sha":null,"homepage":"https://muizidn.github.io/flowconstraint/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/muizidn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-03T14:02:12.000Z","updated_at":"2019-06-01T05:37:11.000Z","dependencies_parsed_at":"2022-08-28T07:12:26.228Z","dependency_job_id":null,"html_url":"https://github.com/muizidn/flowconstraint","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muizidn%2Fflowconstraint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muizidn%2Fflowconstraint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muizidn%2Fflowconstraint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muizidn%2Fflowconstraint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muizidn","download_url":"https://codeload.github.com/muizidn/flowconstraint/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312084,"owners_count":20918341,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autolayout","chain","constraint","fluent","microlibrary","swift"],"created_at":"2025-02-10T18:14:41.620Z","updated_at":"2025-04-05T08:43:17.411Z","avatar_url":"https://github.com/muizidn.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flow Constraint\n\nNo more too verbose constraint! Write it like a flow!\n\n[![Build Status](https://travis-ci.org/muizidn/flowconstraint.svg?branch=master)](https://travis-ci.org/muizidn/flowconstraint)\n[![codecov](https://codecov.io/gh/muizidn/flowconstraint/branch/master/graph/badge.svg)](https://codecov.io/gh/muizidn/flowconstraint)\n![GitHub top language](https://img.shields.io/github/languages/top/muizidn/flowconstraint.svg?color=orange)\n![Cocoapods](https://img.shields.io/cocoapods/v/flowconstraint.svg?color=red)\n\nThis simple library mainly designed to tackle repetitive and too verbose code when simply setup constraint. Many 3rd party library already put syntactic sugar to build constraint but still, some will end up in repetitive and verbose enough code. Unless you make a hack of it.\n\n## Comparison\n\n```swift\nlet red = UIView()\nlet yellow = UIView()\n\nred.addSubview(yellow)\n```\n\n#### NSLayoutConstraint\n```swift\n// a must set property\nyellow.translatesAutoresizingMaskIntoConstraints = false\nlet constraint = NSLayoutConstraint(\n    item: yellow,\n    attribute: .left,\n    relatedBy: .equal,\n    toItem: red,\n    attribute: .left,\n    multiplier: 1,\n    constant: 20\n)\nconstraint.isActive = true\n```\n\n#### NSLayoutAnchor\n```swift\n// a must set property\nyellow.translatesAutoresizingMaskIntoConstraints = false\nlet constraint = yellow.leftAnchor.constraint(equalTo: red.leftAnchor, constant: 20)\nconstraint.isActive = true\n```\n\nIf you want to set **yellow** edges to **red** edges only, you will need a lot of code, and those are repetitive task, you want to get rid of it! If you don't, I do 😄!\n\n#### Flow Constraint\n```swift\nyellow.flow()\n    .left()\n    .withSuperview()\n```\n\nLet's split up the steps to build a constraint.\n1. Create Flow instance to govern how do you create your constraint.\n    \u003e class `Flow` is designed to handle all `dirty` interfaces to build you layout.\n\n2. Constraint setup.\nThere are three groups of methods available\n    - Constraint\n        \u003e Interfaces to govern your Constraint\n\n        example :\n            a. left()\n            b. top(.bottom)\n    \n    - Attribute\n        \u003e Interface to set Constraint.Attribute\n    \n        example :\n            a. attribute(constant: -20)\n            b. attribute(relation: .equal)\n\n    - TargetView\n        \u003e Interface to finalize Constraint setup\n    \n        example :\n            a. withSuperview()\n            b. withSelf()\n\n**Constraint** and **Attribute** interfaces will need to be finalized. Every invocation will return current Flow instance which the method not marked as `@discardableResult`. This is correct. Since before any invocation of **TargetView** interface, all declared constraint not implemented using NSLayoutConstraint under the hood yet.\n\nNOTE: Any invocation of **TargetView** interface **will have side-effect**. Invocation of **TargetView** is supposed to be happen after all constraint match and fixed. It will enumerate all constraint definition inside Flow instance to *implement using **NSLayoutConstraint** under the hood and then remove it after*. So you can get empty and cleaned up Flow instance after any invocation of **TargetView** interface to define other constraint.\n\n#### Example\nTypically, to build edge constraint you will declare in this form in another library or using NSLayoutAnchor. This will be written in FlowConstraint equivalent.\n```swift\nyellow.flow().left().atrribute(constant: 20).withSuperview()\nyellow.flow().top().atrribute(constant: 20).withSuperview()\nyellow.flow().right().atrribute(constant: -20).withSuperview()\nyellow.flow().bottom().atrribute(constant: -20).withSuperview()\n```\n\nYou see that `flow()`, `attribute(constant:)`, `withSuperview()` is repetitively called. It's too verbose though it may make the code more clear 😃. I don't like it.\n\nSome library provide helper method with `edges` like `SnapKit` to avoid this code. But most of them will make it harder to declare constant attribute to each constraint. What if you want to make `bottom` to 200 instead of -20.\n\nThis if you want to make it in FlowConstraint\n```swift\nyellow.flow()\n    .left()\n    .top()\n    .attribute(constant: 20)\n    .withSuperview()\n    // It's okay to add line spacing. Swift is powerfull!\n    .right()\n    .bottom()\n    .attribute(constant: -20)\n    .withSuperview()\n```\nOr\n```swift\nyellow.flow()\n    .left()\n    .top()\n    .right()\n    .bottom()\n    .attribute(constant: 20, for: [.left, .top])\n    .attribute(constant: -20, for: [.right, .bottom])\n    .withSuperview()\n```\nAhh, sure FlowConstraint has been provided `edges` for you. Just check it!\n\n## CocoaPod\nNot yet ready!\n\n## Contribute\nPlease make your Pull Request. I'll happy to review!\n\n## Other Libraries\n- [ViewDSL](http://github.com/muizidn/viewdsl) Combine DSL with FlowConstraint! Beautiful!\n- [EventClosure](http://github.com/muizidn/eventclosure) No more #selector on you UIControl.Event handling. Use closure!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuizidn%2Fflowconstraint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuizidn%2Fflowconstraint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuizidn%2Fflowconstraint/lists"}