Skip to content

Programmatically create views in swift using NSLayoutAnchor or Snapkit

Notifications You must be signed in to change notification settings

nbhasin2/SnapkitSample

Repository files navigation

SnapkitSample

Programmatically create views using NSLayoutAnchor and Snapkit in Swift, XCode 11.4.1

This is a simple repo to show how you can easily programatically create views using Snapkit and NSLayoutAnchor.

How to run

  • Clone the repo
  • Open the project SnapKitSample in XCode
  • Run

How to toggle between three view controllers

  • Open the project SnapKitSample in XCode
  • Go to SceneDelegate.swift
  • Change the rootview controller to the view controller you want to play around with
  • Run
// Toggle between:
// AnchorConstraintViewController()
// SnapkitViewController()
// let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
// let mainStoryboardViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainStoryboardViewController")
// Comment the ones you don't want to show.
// window?.rootViewController = mainStoryboardViewController // MainStoryboardViewController 
// window?.rootViewController = SnapkitViewController() // SnapkitViewController
window?.rootViewController = AnchorConstraintViewController() // AnchorConstraintViewController

There is a BaseConstraintViewController which has all the views initialized in a closure format. These views are used in AnchorConstraintViewController and SnapkitViewController as they are subclasses of BaseConstraintViewController. Both the viewcontrollers have same views and same set of constraints to allow users to understand how Snapkit and NSLayoutAnchor work and their similarities

AnchorConstraintViewController

  • Contains constraints made programatically via NSLayoutAnchor

SnapkitViewController

  • Contains constraints made programatically via Snapkit

MainStoryboardViewController

  • Containts constraints made in storyboard xib
  • Has viewcontroller xib in a storyboard Main.storyboard and its counterpart MainStoryboardViewController.swift
  • Together it shows how you can have a view controller in a storyboard instantiated via SceneDelegate

alt text

About

Programmatically create views in swift using NSLayoutAnchor or Snapkit

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages