create a todo list using snapkit in swift

Here's an example of how to create a basic todo list using SnapKit:

main.swift
import UIKit
import SnapKit

class ViewController: UIViewController {

    var todoList: [String] = [] // list to hold todo items
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let todoTextField = UITextField()
        todoTextField.placeholder = "Enter a todo item"
        view.addSubview(todoTextField)
        
        let addButton = UIButton()
        addButton.setTitle("Add", for: .normal)
        addButton.setTitleColor(.blue, for: .normal)
        addButton.addTarget(self, action: #selector(addTodoItem), for: .touchUpInside)
        view.addSubview(addButton)
        
        let todoListLabel = UILabel()
        todoListLabel.text = "Todo List:"
        view.addSubview(todoListLabel)
        
        let todoListView = UITextView()
        todoListView.isEditable = false
        view.addSubview(todoListView)
        
        // Add constraints using SnapKit
        todoTextField.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalTo(addButton.snp.left).offset(-20)
            make.height.equalTo(40)
        }
        
        addButton.snp.makeConstraints { make in
            make.top.equalTo(todoTextField.snp.top)
            make.right.equalToSuperview().offset(-20)
            make.width.equalTo(80)
            make.height.equalTo(40)
        }
        
        todoListLabel.snp.makeConstraints { make in
            make.top.equalTo(todoTextField.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
        }
        
        todoListView.snp.makeConstraints { make in
            make.top.equalTo(todoListLabel.snp.bottom).offset(10)
            make.left.right.bottom.equalToSuperview().inset(20)
        }
    }
    
    @objc func addTodoItem() {
        guard let todoText = todoTextField.text, !todoText.isEmpty else { return }
        todoList.append(todoText)
        updateTodoListView()
        todoTextField.text = ""
    }
    
    func updateTodoListView() {
        var todoText = ""
        for i in 0..<todoList.count {
            todoText += "\(i+1). \(todoList[i])\n"
        }
        todoListView.text = todoText
    }
}
2260 chars
70 lines

In this example, we create a UITextField for entering todo items, a UIButton for adding the todo item to the list, a UILabel to display the "Todo List" title, and a UITextView to display the actual list of todo items.

Notice that we add constraints using SnapKit, which allows us to easily define layout constraints in code.

We also have a todoList array to hold the actual list of todo items, and two helper methods: addTodoItem to add a new todo item to the list, and updateTodoListView to update the UITextView with the latest list of items.

Overall, this example should give you a good starting point for creating your own todo list using SnapKit in Swift.

gistlibby LogSnag