SkillAgentSearch skills...

Eureka

Elegant iOS form builder in Swift

Install / Use

/learn @xmartlabs/Eureka

README

Eureka: Elegant form builder in Swift

<p align="center"> <a href="https://travis-ci.org/xmartlabs/Eureka"><img src="https://travis-ci.org/xmartlabs/Eureka.svg?branch=master" alt="Build status" /></a> <img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" /> <a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift5-compatible-4BC51D.svg?style=flat" alt="Swift 5 compatible" /></a> <a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a> <a href="https://cocoapods.org/pods/Eureka"><img src="https://img.shields.io/cocoapods/v/Eureka.svg" alt="CocoaPods compatible" /></a> <a href="https://raw.githubusercontent.com/xmartlabs/Eureka/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a> <a href="https://codebeat.co/projects/github-com-xmartlabs-eureka"><img alt="codebeat badge" src="https://codebeat.co/badges/16f29afb-f072-4633-9497-333c6eb71263" /></a> </p>

Made with ❤️ by XMARTLABS. This is the re-creation of [XLForm] in Swift.

简体中文

Overview

<table> <tr> <th> <img src="Example/Media/EurekaExample1.gif" width="220"/> </th> <th> <img src="Example/Media/EurekaExample2.gif" width="220"/> </th> <th> <img src="Example/Media/EurekaExample3.gif" width="220"/> </th> </tr> </table>

Contents

  • [Requirements]
  • [Usage]
    • [How to create a Form]
    • [Getting row values]
    • [Operators]
    • [Using the callbacks]
    • [Section Header and Footer]
    • [Dynamically hide and show rows (or sections)]
    • [List sections]
    • [Multivalued sections]
    • [Validations]
    • [Swipe Actions]
  • [Custom rows]
    • [Basic custom rows]
    • [Custom inline rows]
    • [Custom presenter rows]
  • [Row catalog]
  • [Installation]
  • [FAQ]

For more information look at [our blog post] that introduces Eureka.

Requirements (for latest release)

  • Xcode 11+
  • Swift 5.0+

Example project

You can clone and run the Example project to see examples of most of Eureka's features.

<table> <tr> <th> <img src="Example/Media/EurekaNavigation.gif" width="200"/> </th> <th> <img src="Example/Media/EurekaRows.gif" width="200"/> </th> </tr> </table>

Usage

How to create a form

By extending FormViewController you can then simply add sections and rows to the form variable.

import Eureka

class MyFormViewController: FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        form +++ Section("Section1")
            <<< TextRow(){ row in
                row.title = "Text Row"
                row.placeholder = "Enter text here"
            }
            <<< PhoneRow(){
                $0.title = "Phone Row"
                $0.placeholder = "And numbers here"
            }
        +++ Section("Section2")
            <<< DateRow(){
                $0.title = "Date Row"
                $0.value = Date(timeIntervalSinceReferenceDate: 0)
            }
    }
}

In the example we create two sections with standard rows, the result is this:

<center> <img src="Example/Media/EurekaHowTo.gif" width="200" alt="Screenshot of Custom Cells"/> </center>

You could create a form by just setting up the form property by yourself without extending from FormViewController but this method is typically more convenient.

Configuring the keyboard navigation accesory

To change the behaviour of this you should set the navigation options of your controller. The FormViewController has a navigationOptions variable which is an enum and can have one or more of the following values:

  • disabled: no view at all
  • enabled: enable view at the bottom
  • stopDisabledRow: if the navigation should stop when the next row is disabled
  • skipCanNotBecomeFirstResponderRow: if the navigation should skip the rows that return false to canBecomeFirstResponder()

The default value is enabled & skipCanNotBecomeFirstResponderRow

To enable smooth scrolling to off-screen rows, enable it via the animateScroll property. By default, the FormViewController jumps immediately between rows when the user hits the next or previous buttons in the keyboard navigation accesory, including when the next row is off screen.

To set the amount of space between the keyboard and the highlighted row following a navigation event, set the rowKeyboardSpacing property. By default, when the form scrolls to an offscreen view no space will be left between the top of the keyboard and the bottom of the row.

class MyFormViewController: FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        form = ...

	// Enables the navigation accessory and stops navigation when a disabled row is encountered
	navigationOptions = RowNavigationOptions.Enabled.union(.StopDisabledRow)
	// Enables smooth scrolling on navigation to off-screen rows
	animateScroll = true
	// Leaves 20pt of space between the keyboard and the highlighted row after scrolling to an off screen row
	rowKeyboardSpacing = 20
    }
}

If you want to change the whole navigation accessory view, you will have to override the navigationAccessoryView variable in your subclass of FormViewController.

Getting row values

The Row object holds a value of a specific type. For example, a SwitchRow holds a Bool value, while a TextRow holds a String value.

// Get the value of a single row
let row: TextRow? = form.rowBy(tag: "MyRowTag")
let value = row.value

// Get the value of all rows which have a Tag assigned
// The dictionary contains the 'rowTag':value pairs.
let valuesDictionary = form.values()

Operators

Eureka includes custom operators to make form creation easy:

+++       Add a section

form +++ Section()

// Chain it to add multiple Sections
form +++ Section("First Section") +++ Section("Another Section")

// Or use it with rows and get a blank section for free
form +++ TextRow()
     +++ TextRow()  // Each row will be on a separate section

<<<       Insert a row

form +++ Section()
        <<< TextRow()
        <<< DateRow()

// Or implicitly create the Section
form +++ TextRow()
        <<< DateRow()

+=        Append an array

// Append Sections into a Form
form += [Section("A"), Section("B"), Section("C")]

// Append Rows into a Section
section += [TextRow(), DateRow()]

Result builders

Eureka includes result builders to make form creation easy:

@SectionBuilder

// Section + Section
form = (Section("A") +++ {
    URLRow("UrlRow_f1") { $0.title = "Url" }
    if something {
        TwitterRow("TwitterRow_f2") { $0.title = "Twitter" }
    } else {
        TwitterRow("TwitterRow_f1") { $0.title = "Twitter" }
    }
    AccountRow("AccountRow_f1") { $0.title = "Account" }
})

// Form + Section
form +++ {
    if something {
        PhoneRow("PhoneRow_f1") { $0.title = "Phone" }
    } else {
        PhoneRow("PhoneRow_f2") { $0.title = "Phone" }
    }
    PasswordRow("PasswordRow_f1") { $0.title = "Password" }
}

@FormBuilder

@FormBuilder
var form: Form {
    Section("Section A") { section in
        section.tag = "Section_A"
    }
    if true {
        Section("Section B") { section in
            section.tag = "Section_B"
        }
    }
    NameRow("NameRow_f1") { $0.title = "Name" }
}

Using the callbacks

Eureka includes callbacks to change the appearance and behavior of a row.

Understanding Row and Cell

A Row is an abstraction Eureka uses which holds a value and contains the view Cell. The Cell manages the view and subclasses UITableViewCell.

Here is an example:

let row  = SwitchRow("SwitchRow") { row in      // initializer
                        row.title = "The title"
                    }.onChange { row in
                        row.title = (row.value ?? false) ? "The title expands when on" : "The title"
                        row.updateCell()
                    }.cellSetup { cell, row in
                        cell.backgroundColor = .lightGray
                    }.cellUpdate { cell, row in
                        cell.textLabel?.font = .italicSystemFont(ofSize: 18.0)
                }
<img src="Example/Media/EurekaOnChange.gif" width="300" alt="Screenshot of Disabled Row"/>

Callbacks list

  • onChange()

    Called when the value of a row changes. You might be interested in adjusting some parameters here or even make some other rows appear or disappear.

  • onCellSelection()

    Called each time the user taps on the row and it gets selected. Note that this will also get called for disabled rows so you should start your code inside this callback with something like guard !row.isDisabled else { return }

  • cellSetup()

    Called only once when the cell is first configured. Set permanent settings here.

  • cellUpdate()

    Called each time the cell appears on screen. You can change the appearance here using variables that may not be present on cellSetup().

  • onCellHighlightChanged()

    Called whenever the cell or any subview become or resign the first responder.

  • onRowValidationChanged()

    Called whenever the the validation errors associated with a row changes.

  • onExpandInlineRow()

    Called before expanding the inline row. Applies to rows conforming InlineRowType protocol.

  • onCollapseInlineRow()

    Called before collapsing the inline row. Applies to rows conforming InlineRowType protocol.

  • onPresent()

    Called by a row just before presenting another view controller. Applies to rows conforming PresenterRowType protocol. Use it to set up the presente

Related Skills

View on GitHub
GitHub Stars11.8k
CategoryDevelopment
Updated3h ago
Forks1.3k

Languages

Swift

Security Score

100/100

Audited on Mar 31, 2026

No findings