Skip to content

πŸ’¬ A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code.

License

Notifications You must be signed in to change notification settings

mezhevikin/AlertController

Repository files navigation

AlertController

πŸ’¬ A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code.

Alert

let alert = UIAlertController.alert()
alert.setTitle("βœ… Success", color: .darkGreen)
alert.setMessage("Your message has been sent")
alert.addAction(
    title: "Send more",
    systemIcon: "envelope.fill",
    color: .darkGreen,
    leftAligment: true
) {}
alert.addAction(
    title: "Delete message",
    systemIcon: "trash.fill",
    color: .red,
    leftAligment: true
) {}
alert.addOkAction()
present(alert, animated: true)

Sheet

let sheet = UIAlertController.sheet("πŸ‘¨πŸ» Mezhevikin Alexey")
sheet.addAction(
    title: "Edit profile",
    systemIcon: "person.fill",
    color: .darkGreen,
    leftAligment: true
) {}
sheet.addAction(
    title: "Delete account",
    systemIcon: "trash.fill",
    color: .red,
    leftAligment: true
) {}
sheet.addAction(
    title: "Log out",
    systemIcon: "square.and.arrow.down.fill",
    leftAligment: true
) {}
sheet.addCancelAction()
present(sheet, sourceView: cell)

Choice

let sheet = UIAlertController.sheet("Choose your favorite animal")
let animals = ["🐈 Cat", "πŸ• Dog", "🐎 Horse", "🐫 Camel"]
for (i, animal) in animals.enumerated() {
    sheet.addAction(
        title: animal,
        checked: favoriteAnimal == i,
        leftAligment: true
    ) {
        self.favoriteAnimal = i
    }
 }
sheet.addCancelAction()
present(sheet, sourceView: cell)

TextField

let alert = UIAlertController.alert("πŸ”“ Login")
alert.addTextField {
    $0.placeholder = "βœ‰οΈ Mail"
}
alert.addTextField {
    $0.placeholder = "πŸ”‘ Password"
    $0.isSecureTextEntry = true
}
alert.addAction(title: "OK") {
    if let mail = alert.textFields?[0].text,
       let password = alert.textFields?[1].text
    {
        print("βœ‰οΈ \(mail), πŸ”‘ \(password)")    
    }
}
present(alert)

Present

// Alert
present(alert)
// Sheet from cell with iPad support
present(sheet, sourceView: cell) 
// Sheet from BarButton with iPad support
present(sheet, barButtonItem: navigationItem.leftBarButtonItem) 

Swift Package Manager

https://github.com/mezhevikin/AlertController.git

CocoaPods

pod 'AlertController', :git => 'https://github.com/mezhevikin/AlertController.git'