Coder Social home page Coder Social logo

layoutaid's Introduction

Tests

LayoutAid

AutoLayout extensions for iOS, macOS, and tvOS.

Features

  • Declarative DSL for creating layout constraints
  • keyboardLayoutGuide for UIView on iOS, fully animatable
  • keyboardSafeAreaLayoutGuide for UIView on iOS, fully animatable
  • Extensions for UIScrollView adjusting contentInset based on keyboard appearance

Requirements

  • iOS 11+
  • tvOS 11+
  • macOS 10.11+

Installation

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/LayoutAid.framework to an iOS project.

github "davidask/LayoutAid"

Swift Package Manager

This project has support for Swift Package Manager.

Usage

To use the extensions provided in this library, you must import LayoutAid.

Layout DSL

NSLayoutConstraint.activate {
    containerView.anchor(
        Edges(equalTo: view.layoutMarginsGuide)
    )

    iconView.anchor {
        Width(equalTo: 50)
        AspectRatio(equalTo: 1)
        Center(equalTo: containerView)
    }

    label.anchor {
        Top(equalToSystemSpacingBelow: iconView)
        CenterX(equalTo: containerView.readableContentGuide)
        Width(lessThanOrEqualTo: containerView.readableContentGuide, multiplier: 0.5)
    }
}

Constraints are best activated in bulk, however, creating complex layouts can get verbose, even with layout anchors. This library provides two static methods on NSLayoutConstraint using function builders:

  • build, for creating constraints using a function builder
  • activate, for creating and activating constraints using a function builder

To constrain a view or layout guide use view.anchor or layoutGuide.anchor. Constrain blocks can be used inside NSLayoutConstraint.build or NSLayoutConstraint.activate.

This library aligns its semantics with Apples layout anchor API, meaning that you'll find a constraint builder for each anchor type, including convenience builders.

  • Leading
  • Trailing
  • Left
  • Right
  • Top
  • Bottom
  • CenterX
  • CenterY
  • Center
  • Edges
  • DirectionalEdges
  • Size
  • AspectRatio

Keyboard layout guides

Keyboard management in iOS can be tricky. This library provides a lazy accessor to keyboardLayoutGuide and keyboardSafeAreaLayoutGuide. Backed by a Keyboard type observing the keyboard state this allows you to easily layout your views with the keyboard in mind.

NSLayoutConstraint.activate {
    keyboardBackgroundView.anchor(
        Edges(equalTo: view.keyboardLayoutGuide)
    )

    keyboardAvoidingView.anchor {
        Leading(equalTo: view)
        Trailing(equalTo: view)
        Top(equalTo: view.safeAreaLayoutGuide)
        Bottom(lessThanOrEqualTo: view.keyboardSafeAreaLayoutGuide)
    }
}

UIScrollView extensions

This library can automatically adjust contentInset of UIScrollView based on keyboard appearance using

  • UIScrollView.adjustContentInsetForKeyboard(), to immediately adjust scroll view insets to keyboard
  • UIScrollView.beginAdjustingContentInsetForKeyboard(), to start observing keyboard adjusting scroll view insets automatically
  • UIScrollView.endAdjustingContentInsetForKeyboard(), to stop observing keyboard adjusting scroll view insets automatically

Contribute

Please feel welcome contributing to LayoutAid, check the LICENSE file for more info.

Credits

David Ask

layoutaid's People

Contributors

davidask avatar

Stargazers

Franklin Byaruhanga avatar  avatar

Watchers

James Cloos avatar  avatar  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.