/PrettyConstraints

Create your views faster programmatically using PrettyConstraints

Primary LanguageSwiftMIT LicenseMIT

PrettyConstraints

PrettyConstraints helps you create your views faster programmatically using AutoLayout constraints.

Installation

CocoaPods

Using CocoaPods, install it by adding the following line to your Podfile:

pod 'PrettyConstraints'

Swift Package Manager

Using Swift Package Manager, add it as a Swift Package in Xcode 11.0 or later, select File > Swift Packages > Add Package Dependency... and add the repository URL:

https://github.com/mobiraft/PrettyConstraints

Manual Installation

Download and include the PrettyConstraints folder and files in your codebase.

Requirements

  • iOS 11
  • Swift 5

How To Use

Embedding View in another View:

let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 100)))
view.backgroundColor = .black
let subView = UIView()
subView.backgroundColor = .gray
view.addSubview(subView)
subView.applyConstraints(.fitInView(view, inset: 10))

Centering View inside another View:

subView.applyConstraints(.center(in: view),
                         .width(constant: 50),
                         .height(constant: 50))

Other Constraints examples:

subView.applyConstraints(.top(to: view.topAnchor),
                         .bottom(to: view.bottomAnchor),
                         .leading(to: view.leadingAnchor, equality: .greaterThanOrEqual),
                         .trailing(to: view.trailingAnchor, equality: .lessThanOrEqual),
                         .centerX(to: view.centerXAnchor),
                         .width(constant: 50),
                         .height(constant: 50))

Store constraints and modify when needed:

let constraints:[NSLayoutConstraint] = subView.applyConstraints(.center(in: view),
                                                                .width(constant: 50),
                                                                .height(constant: 50))
constraints[1].constant = 20 // Modifies the width as 20.0

Blog

Contant us

Twitter

Website

Visit us for more Android, SwiftUI and Swift articles. www.mobiraft.com