GithubHelp home page GithubHelp logo

denislitvin / pinner Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 83 KB

๐Ÿ“Œ Auto-Layout wrapper (DSL) for more concise and swiftier syntax.

License: MIT License

Swift 85.95% Ruby 14.05%
pinner swift constraints layout ui uikit uiview anchors xcode auto

pinner's Introduction

Pinner

CI Status Version License Platform

Usage

If you you want to see the full example, run the example project, clone the repo, and run pod install from the Example directory first.

Pinner

All constraints are specified as the input parameters. Call the method of UIView - makeConstraints(for:) which takes one or more instances of CSMConstraintType enumeration, separated with commas.

enum CSMConstraintType {
    case top
    case leading
    case left
    case bottom
    case trailing
    case right

    case height
    case width

    case centerX
    case centerY
}

All calculations are made in the closure. You can easily pin the view to any side of it's superview with pin(to:), method of the instance of CSMConstraintPinner, or pinAndReturn(to:) if you want to use the constraint later. Constants are made by using equal(_:) or equalAndReturn(_:) methods.

Input parameters for pin methods (use in that order):

  • _ anchor : NSLayoutAnchor - anchor point.
  • const : CGFloat - constant offset in certain axis.
  • mult : CGFLoat - multiplier, uses only for height and width anchors.
  • options : CSMConstraintOptions - possible options: .equal, .lessOrEqual, .moreOrEqual.

By default you only need to specify the anchor, all constraints are activated and underlying view has it's property translatesAutoresizingMaskIntoConstraints set to false!

Input parameter for equal methods is only the constant(CGFloat).

You also have an ability to return all anchors within a closure by using returnAll() method on CSMConstraintPinner. And you can deactivate your constraints using deactivate(_ :) with the index of constraint, or use deactivateAll().

Example

someView.makeConstraints(for: .top, .left, .width, .height) { (make) in
    self.someViewTopAnchor = make.pinAndReturn(to: self.view.topAnchor, const: 30)
    make.pin(to: self.view.leftAnchor, const: 10)
    make.pin(to: self.view.widthAnchor, mult: 0.3)
    make.equal(200)
}

Helper functions

  1. Arrange view based on Safe Area Layout Guide that works for all devices and ios versions:

viewController.fillSafeArea(with: someView)
  1. If you want to cover the entire superview:
someView.fillSuperview()
  1. Anchor to center of a view:
someView.anchorCenterSuperview()

or

someView.anchorCenterXToSuperview(constant: 10)
someView.anchorCenterYToSuperview(constant: 5)

Installation

CocoaPods:

Add the line pod "Pinner" to your Podfile

Manual:

Clone the repo and drag the file Pinner.swift into your Xcode project.

Platform support

  • iOS 9.3 and above

Author

DenisLitvin, [email protected]

License

Pinner is available under the MIT license. See the LICENSE file for more info.

pinner's People

Contributors

denislitvin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.