Stevia Documentation

repository·master·Indexed 25 days ago

https://github.com/freshos/stevia

A Swift-based visual layout engine for iOS and tvOS providing a chainable, equation-based API alternative to Auto Layout. Features a DSL for defining view hierarchies, layout constraints, and styling blocks, with support for installation via Swift Package Manager.

Tokens
747
Snippets
5
Records
7
Agent score
36%

What's inside Stevia

  1. Install Stevia via Swift Package Manager

    master

    Stevia is installed using the official Swift Package Manager (SPM).

    1. In Xcode, go to File > Swift Packages > Add Package Dependency....
    2. Enter the repository URL: https://github.com/freshOS/Stevia.

    Note: Other package managers like Carthage or CocoaPods are deprecated as of version 4.8.0.

  2. Use the Equation-Based API

    master

    For more precise control, use an equation-based syntax to define constraints between views or relative to container dimensions. You can also set layout priorities.

    email.Top == 100
    password.CenterY == forgot.CenterY
    login.Top >= password.Bottom + 20
    login.Width == 75 % Width
    (image.Height == 100).priority = UILayoutPriority(rawValue: 999)
  3. Apply Styling with the style block

    master

    Use the style method to group property assignments (like fonts, colors, or keyboard types) for a view into a single, readable block.

    email.style { f in
        f.borderStyle = .roundedRect
        f.autocorrectionType = .no
        f.keyboardType = .emailAddress
        f.font = UIFont(name: "HelveticaNeue-Light", size: 26)
        f.returnKeyType = .next
    }
  4. Use the Visual Layout API

    master

    Stevia provides a highly readable DSL for defining view hierarchies and layouts. You can define spacing, view dimensions, and relative positioning using a concise syntax that generates native NSLayoutConstraints.

    layout {
        100
        |-email-| ~ 80
        10%
        |-password-forgot-| ~ 80
        >=20
        |login| ~ 80
        0
    }
  5. Use the Chainable API

    master

    You can chain layout methods directly on views to set properties like position, size, and alignment.

    email.top(100).left(8).right(8).width(200).height(44)
    alignHorizontally(password, forgot)
    image.fillContainer()
    button.centerInContainer().size(50%)
    equalWidths(email, password)
    image.width(>=80)
  6. Manage View Hierarchy with subviews

    master

    Instead of manually setting translatesAutoresizingMaskIntoConstraints = false and calling addSubview() for every view, use the subviews block to manage the hierarchy concisely.

    subviews {
        email
        password
        login
    }