Coder Social home page Coder Social logo

pisces / simplelayout Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 155 KB

SimpleLayout helps you to using auto layout very easily

License: MIT License

Ruby 4.24% Objective-C 2.01% Swift 93.75%
autolayout carthage simplelayout xcode swift iphone-x iphonex safearealayoutguide ios11

simplelayout's Introduction

SimpleLayout

SimpleLayout helps you to using auto layout very easily

Swift CI Status Version License Platform Carthage Compatible

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Difference in code

Using auto layout with pure iOS SDK

let top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: -10)
let trailing = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 10)
let width = NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
let height = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)

view.addConstraints([width, height])
view.layoutIfNeeded()
self.addConstraints([top, trailing])
self.layoutIfNeeded()

Using auto layout with SimpleLayout

view.layout
    .top(-10)
    .trailing(10)
    .width(fixed: 30)
    .height(fixed: 30)

Using SafeAreaLayoutGuide for iPhone X (iOS 11 or higher)

view.layout
    .top(by: view._safeAreaLayoutGuide)
    .leading()
    .trailing()
    .bottom(by: view._safeAreaLayoutGuide)

To implement auto layout using the fill method

import UIKit
import SimpleLayout_Swift

class FillExampleViewController: UIViewController {

    private lazy var subview: UIView = {
        let label = UILabel()
        label.text = "subview"
        label.textColor = .white

        let subview = UIView()
        subview.backgroundColor = .red

        subview.addSubview(label)
        label.layout
            .leading()
            .top()
            .width(fixed: 0, relation: .greaterThanOrEqual)
            .height(fixed: 0, relation: .greaterThanOrEqual)
        return subview
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        edgesForExtendedLayout = .bottom

        let label = UILabel()
        label.text = "view"

        view.addSubview(label)
        view.addSubview(subview)
        label.layout
            .leading()
            .top()
            .width(fixed: 0, relation: .greaterThanOrEqual)
            .height(fixed: 0, relation: .greaterThanOrEqual)
        subview.layout.fill(leading: 30, top: 30, trailing: -30, bottom: -30)
    }
}

To implement auto layout using the property chain

import UIKit
import SimpleLayout_Swift

class ChainExampleViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        edgesForExtendedLayout = .bottom

        let subview1 = label(backgroundColor: .red, text: "subview1")
        let subview2 = label(backgroundColor: .purple, text: "subview2")
        let subview3 = label(backgroundColor: .blue, text: "subview3")

        view.addSubview(subview1)
        view.addSubview(subview2)
        view.addSubview(subview3)

        subview1.layout
            .leading()
            .top()
            .width()
            .height(fixed: 150)
        subview2.layout
            .leading(by: subview1)
            .top(by: subview1, attribute: .bottom)
            .width(fixed: view.width/2)
            .bottom()
        subview3.layout
            .leading(by: subview2, attribute: .trailing)
            .top(by: subview2)
            .trailing()
            .bottom(by: view.layout.safeAreaLayoutGuide)
    }

    private func label(backgroundColor: UIColor, text: String) -> UILabel {
        let label = UILabel()
        label.backgroundColor = backgroundColor
        label.text = text
        label.textAlignment = .center
        label.textColor = .white
        return label
    }
}

Requirements

iOS SDK 9.0 equal or higher

Installation

SimpleLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SimpleLayout-Swift"

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SimpleLayout into your Xcode project using Carthage, specify it in your Cartfile:

github "pisces/SimpleLayout" ~> 1.2.1

Run carthage update to build the framework and drag the built SimpleLayout.framework into your Xcode project.

Author

Steve Kim, [email protected]

License

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

simplelayout's People

Contributors

pisces avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

venticake

simplelayout's Issues

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.