Skip to content
Gabriel Theodoropoulos edited this page May 28, 2019 · 1 revision

GTEasyLayout Usage Examples

Consider the following class with the two labels implemented in code:

class ViewController: UIViewController, GTEasyLayoutAdaptable {
    lazy var testView1: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "View #1"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 34.0)
        label.textAlignment = .center
        label.backgroundColor = UIColor.darkGray
        return label
    }()


    lazy var testView2: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "View #2"
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 34.0)
        label.textAlignment = .center
        label.backgroundColor = UIColor.lightGray
        return label
    }()

    // ...
}

Here are some examples with visual representation of the results in the Simulator:

// Snap view to all edges leaving 50px padding from all sides.
add(view: testView1,
     to: self.view,
     snapTo: .all,
     padding: UIEdgeInsets(all: 50.0),
     size: .zero)


// Snap to top, left, bottom and set the width to 250px.
// Height will be 0.0, as it's calculated automatically.
add(view: testView1,
     to: self.view,
     snapTo: .topLeftBottom,
     padding: UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0),
     size: CGSize(width: 250.0, height: 0.0))


// Center both horizontally and vertically.
// View size is 200x150px.
add(view: testView1,
     to: self.view,
     snapTo: .centerX_Y,
     padding: .zero,
     size: CGSize(width: 200.0, height: 150.0))


// Snap two views to top-left-right and bottom-left-right.
// Height is the same in both (150px).
add(view: testView1,
     to: self.view,
     snapTo: .topRightLeft,
     padding: UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0),
     size: CGSize(width: 0.0, height: 150.0))

add(view: testView2,
     to: self.view,
     snapTo: .bottomRightLeft,
     padding: .zero,
     size: CGSize(width: 0.0, height: 150.0))


// Update the left constraint (left padding) of the view.
//
// Initially set it to -250 so the view is positioned
// out of the visible area of the screen.
add(view: testView1,
     to: self.view,
     snapTo: .topLeftBottom,
     padding: UIEdgeInsets(top: 20.0, left: -250.0, bottom: 0.0, right: 0.0),
     size: CGSize(width: 250.0, height: 0.0))

// When the left constraint is about to be updated:
//
// 1. Optionally specify custom animation settings. If you don't
// the default settings will be used instead.
let animSettings = GTEasyLayout.AnimationSettings(withDuration: 0.5,
                                                  delay: 0.0,
                                                  damping: 0.7,
                                                  velocity: 1.0,
                                                  options: .curveLinear)

// 2. Update:
updatePadding(at: .left,
              ofView: testView1,
              newValue: 0.0,
              animationSettings: animSettings,
              completion: nil)


// Update the height of the view by doubling its value.
//
add(view: testView2,
    to: self.view,
    snapTo: .bottomCenterX,
    padding: .zero,
    size: CGSize(width: 300.0, height: 200.0))

// Use the default animation settings this time.
updateDimension(.height,
                ofView: testView2,
                newValue: 400.0,
                animationSettings: nil,
                completion: nil)


Read the documentation of the GTEasyLayoutAdaptable protocol in the GTEasyLayoutAdaptable.swift file, and of the custom types defined in the GTEasyLayout class in the GTEasyLayout.swift file in the Source directory. Alternatively, take a look at this generated documentation by jazzy.

Clone this wiki locally