Customizable simple Alert and simple ActionSheet for Swift

Overview

SimpleAlert

Carthage compatible Version License Platform

It is simple and easily customizable alert. Can be used as UIAlertController.

Appetize's Demo

default_view custom_view custom_content rounded_view

Requirements

  • Swift 5.0
  • iOS 9.0 or later

How to Install SimpleAlert

Cocoapods

Add the following to your Podfile:

pod "SimpleAlert"

Carthage

Add the following to your Cartfile:

github "KyoheiG3/SimpleAlert"

Usage

Example

View simple Alert

let alert = AlertController(title: "title", message: "message", style: .alert)

alert.addTextField()
alert.addAction(AlertAction(title: "Cancel", style: .cancel))
alert.addAction(AlertAction(title: "OK", style: .ok))

present(alert, animated: true, completion: nil)

Customize default contents

let alert = AlertController(title: "title", message: "message", style: .alert)
alert.addTextField { textField in
    textField.frame.size.height = 33
    textField.backgroundColor = nil
    textField.layer.borderColor = nil
    textField.layer.borderWidth = 0
}
alert.configureContentView { view in
    view.titleLabel.textColor = UIColor.lightGrayColor()
    view.titleLabel.font = UIFont.boldSystemFontOfSize(30)
    view.messageLabel.textColor = UIColor.lightGrayColor()
    view.messageLabel.font = UIFont.boldSystemFontOfSize(16)
    view.textBackgroundView.layer.cornerRadius = 3.0
    view.textBackgroundView.clipsToBounds = true
}

alert.addAction(AlertAction(title: "Cancel", style: .cancel))
alert.addAction(AlertAction(title: "OK", style: .ok))
present(alert, animated: true, completion: nil)

Rounded button Alert View

let alert = AlertController(view: UIView(), style: .alert)
alert.contentWidth = 144
alert.contentCornerRadius = 72
alert.contentColor = .white
let action = AlertAction(title: "?", style: .cancel) { action in
}

alert.addAction(action)
action.button.frame.size.height = 144
action.button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 96)
action.button.setTitleColor(UIColor.red, for: .normal)

present(alert, animated: true, completion: nil)

More customizable if you create a subclass

class CustomAlertController: AlertController {
    override func addTextField(configurationHandler: ((UITextField) -> Void)? = nil) {
        super.addTextField { textField in
            textField.frame.size.height = 33
            textField.backgroundColor = nil
            textField.layer.borderColor = nil
            textField.layer.borderWidth = 0

            configurationHandler?(textField)
        }
    }

    override func configureActionButton(_ button: UIButton, at style :AlertAction.Style) {
        super.configureActionButton(button, at: style)

        switch style {
        case .ok:
            button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
            button.setTitleColor(UIColor.gray, for: UIControlState())
        case .cancel:
            button.backgroundColor = UIColor.darkGray
            button.setTitleColor(UIColor.white, for: UIControlState())
        case .default:
            button.setTitleColor(UIColor.lightGray, for: UIControlState())
        default:
            break
        }
    }

    override func configureContentView(_ contentView: AlertContentView) {
        super.configureContentView(contentView)

        contentView.titleLabel.textColor = UIColor.lightGray
        contentView.titleLabel.font = UIFont.boldSystemFont(ofSize: 30)
        contentView.messageLabel.textColor = UIColor.lightGray
        contentView.messageLabel.font = UIFont.boldSystemFont(ofSize: 16)
        contentView.textBackgroundView.layer.cornerRadius = 10.0
        contentView.textBackgroundView.clipsToBounds = true
    }
}

Class

AlertAction

Style

  • default
  • ok
  • cancel
  • destructive

Initialize

init(title: String, style: SimpleAlert.AlertAction.Style, dismissesAlert: Bool = default, handler: ((SimpleAlert.AlertAction?) -> Swift.Void)? = default)
  • Set title and style, can add button.
  • Set button action handler.

Variable

var isEnabled: Bool
  • Set button enabled.
let button: UIButton
  • Can get a button.
  • Can get after button has been added to the AlertController.

AlertContentView

backgroundColor of AlertContentView will be reflected in the overall backgroundColor.

var baseView: UIView!
  • Base view for contents
var titleLabel: UILabel!
  • Title label
var messageLabel: UILabel!
  • Message Label
var textBackgroundView: UIView!
  • Base view for Text Field
  • UIAlertControllerStyle is in the case of actionSheet does not appear.

AlertController

Initialize

init(title: String?, message: String?, style: UIAlertControllerStyle)
  • Set title, message and style, can add button.
  • Set button action handler.
init(title: String? = default, message: String? = default, view: UIView?, style: UIAlertControllerStyle)
  • Can also set custom view.

Variable

open var contentWidth: CGFloat
open var contentColor: UIColor?
open var contentCornerRadius: CGFloat?
open var coverColor: UIColor
open var message: String?
  • Can change alert style.
public private(set) var actions: [SimpleAlert.AlertAction]
public var textFields: [UITextField] { get }
  • Can get actions and text fields that is added.

Function

func addTextField(configurationHandler: ((UITextField) -> Swift.Void)? = default)
  • Add Text Field, and set handler.
  • UIAlertControllerStyle is in the case of actionSheet does not add.
func addAction(_ action: SimpleAlert.AlertAction)
  • Add action button.
func configureActionButton(_ button: UIButton, at style: SimpleAlert.AlertAction.Style)
  • Override if would like to configure action button.
func configureContentView(_ contentView: SimpleAlert.AlertContentView)
  • Override if would like to configure content view.

The difference between default UIAlertController

  • Can add a cancel button any number of the actionSheet.
  • If tap the outside of the view, the action handler will not be executed of the actionSheet.

Author

Kyohei Ito

Follow me πŸŽ‰

LICENSE

Under the MIT license. See LICENSE file for details.

Comments
  • Title & message in AlertController.init(view: ...)

    Title & message in AlertController.init(view: ...)

    Hey @KyoheiG3,

    how can I have the Title + message view still when I use the AlertController.init(view: ...) initializer. It's seems that you replace the 'basic view' with the view passed as parameter, is there any way to keep both views?

    Thanks

    opened by akabab 7
  • can't modify message text

    can't modify message text

    After initializing a simpleAlert controller, it is possible to change the title, but it is not possible to change the message.

    This works: let alert = SimpleAlert.Controller(title: "blah", message: "blah", style: .Alert) alert.title = "changed it"

    But this doesn't: let alert = SimpleAlert.Controller(title: "blah", message: "blah", style: .Alert) alert.message = "changed it"

    For consistency & flexibility, it should be possible to set the message value in the same way as the title.

    opened by grantism 3
  • need to change AlertAction button type to custom instead of system

    need to change AlertAction button type to custom instead of system

    I need this let variable public let button = UIButton(type: .system) in open class AlertAction changed to var variable public var button = UIButton(type: .system) so that I can override the class and can change the button type

    Screen Shot 2019-03-26 at 17 23 13

    Thank you

    opened by sssandyad 2
  • Show Data in CustomView

    Show Data in CustomView

    I want to make a Alert with a UITableView so i use this pod i have use this code (at the bottom) to show the Alert with the CustomView. So TableView2Controller is my tableview but when it show the Alert itΒ΄s shows the TableView but no data in it (cells) . I have tried without the Alert self.showViewController(AlertView, sender: self) itΒ΄s shows the data in the tableview so the problem must be in let AlertTableView = SimpleAlert.Controller(view: AlertView.view, style: .Alert) .
    In my another Alert i have a Button in the view, in my ViewController a Action from the Button and a print int it. When i show the Alert and klick the Button itΒ΄s print anything so i think itΒ΄s the same Problem like the tableviewalert.

    ` let storyboard = UIStoryboard(name: "TableView2Controller", bundle: nil)

        let AlertView = storyboard.instantiateViewControllerWithIdentifier("TableView2Controller") as! TableView2Controller
    
        let AlertTableView = SimpleAlert.Controller(view: AlertView.view, style: .Alert)
    
        showAlert(AlertTableView)` 
    

    So have you an idea what can be the Problem hier ? Thanks for your Help.

    opened by Danielthenew 2
  • No such module SimpleAlert

    No such module SimpleAlert

    I'm getting this error message on the line No such module SimpleAlert

    import SimpleAlert

    I tried adding the version number in the pod file, doesn't help...

    the pod installation worked correctly, no error message. Anyone else having this problem?

    opened by DominikButz 2
  • Hidden custom view when neither of title, message or text field is used

    Hidden custom view when neither of title, message or text field is used

    From version 5.0.0 it is not possible to see the custom view if I don't set also the title or message.

    I try to use it like this

    let alertView = ...
    let alert = AlertController(view: alertView, style: .alert)
    let cancelAction = ...
    let addAction = ...
    alert.addAction(cancelAction)
    alert.addAction(addAction)
    present(alert, animated: true, completion: nil)
    

    But I can see only the buttons.

    I located the problem to be here (and next line also): https://github.com/KyoheiG3/SimpleAlert/blob/057e8330941c8cac798489ce3fbebe8441543476/SimpleAlert/AlertContentView.swift#L29

    opened by DanPetras 1
  • Swift 4.2 support

    Swift 4.2 support

    Hi,

    I am getting error some swift 4.2 changes like UIControlState change to UIControll.state in swift 4.2. Its working fine after changing swift 4 for compilation instead of 4.2. Please update Git after compile your source code with Swift 4.2. I am executing code with Xcode 10.1 through Pod install

    opened by Github-Prashant 1
  • How can I change font of title in alert controller?

    How can I change font of title in alert controller?

    I'm trying to modify the font of the title in alert controller. This is how I create it:

    `let alertController = AlertController(title: nil, message: "this could have some fancy font", style: .actionSheet)`
    

    I know I can change the color of this font by:

        `alertController.view.tintColor = orangeColor`
    

    but what about custom font?

    opened by nalogowiec 1
  • How to dismiss alertcontroller

    How to dismiss alertcontroller

    Keyboard is automatically shown as soon as the alertview with a textfield is used. this is covering the buttons (cancel button added to view) so not possible to dismiss the keyboard or cancel out and dismiss the entire view.

    1. How to remove auto selection on the textfield as the alertview is shown?
    2. Possible to dismiss the alertview by tapping outside of the alertview?

    R

    opened by rezasham 1
  • Allow the cover background color to be configured

    Allow the cover background color to be configured

    This is a small change to allow the cover background colour to be configured as required. The default is set to the same as before.

    Thanks for the brilliant library!

    opened by matt-oakes 1
  • Long title is not fully shown in some languages

    Long title is not fully shown in some languages

    When sets long text to title, title is not fully shown in some languages.

    Example code

    let alert = CustomAlertController(
        title: "μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ? μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?",
        message: "이 μž‘μ—…μ€ μ·¨μ†Œν•  수 μ—†μŠ΅λ‹ˆλ‹€. 이 μž‘μ—…μ€ μ·¨μ†Œν•  수 μ—†μŠ΅λ‹ˆλ‹€.",
        style: .alert
    )
    alert.addAction(.init(title: "OK", style: .default))
    
    self.present(alert, animated: true)
    

    Results

    |Default|Custom with attributed string| |---|---| |Simulator Screen Shot - iPhone 11 Pro - 2022-08-16 at 15 53 21|Simulator Screen Shot - iPhone 11 Pro - 2022-08-16 at 15 53 54|

    opened by yshrkt 0
  • iPad popover

    iPad popover

    Is it possible to add popover support for iPad? Since following code shows empty popover with white area at the bottom.

            if alert.respondsToSelector("popoverPresentationController") {
                alert.modalPresentationStyle = .Popover
                let ppc = alert.popoverPresentationController
                ppc?.barButtonItem = self.navigationItem.rightBarButtonItem
            }
            self.presentViewController(alert, animated: true, completion: nil)
    
    enhancement 
    opened by tosbaha 1
Owner
Kyohei Ito
Kyohei Ito
Swift Package mimicking UIKit's ActionSheet with added features

ActionSheetController A Swift package that mimics the UIAlertController Actionsheet with added features. ActionSheetController gives you the ability t

Adebiyi Mojisola 5 Sep 4, 2021
πŸ“‘ Actionsheet with navigation features such as the Flipboard App

?? SHEET helps you easily create a wide variety of action sheets with navigation features used in the Flipboard App Installation CocoaPods pod 'Sheet'

ArLupin 322 Nov 28, 2022
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.

Introduction Popup Dialog is a simple, customizable popup dialog written in Swift. Features Easy to use API with hardly any boilerplate code Convenien

Orderella Ltd. 3.8k Dec 20, 2022
A Simple Customizable Alert With Swift

SimpleCustomizableAlert trying to make my very first library. Support Title Message (TextView) Image TextField Button Action Example let alert

null 1 Oct 26, 2021
Highly customizable alertview and alert/notification/success/error/alarm popup written in Swift

CDAlertView is highly customizable alert popup written in Swift. Usage is similar to UIAlertController. Screenshots Animations Usage Basic usage witho

Candost Dagdeviren 1.1k Dec 30, 2022
PMAlertController is a great and customizable alert that can substitute UIAlertController

PMAlertController is a small library that allows you to substitute Apple's uncustomizable UIAlertController, with a beautiful and totally customizable

Paolo Musolino 2.5k Jan 3, 2023
PMAlertController is a great and customizable alert that can substitute UIAlertController

PMAlertController is a small library that allows you to substitute Apple's uncustomizable UIAlertController, with a beautiful and totally customizable

Paolo Musolino 2.5k Jan 3, 2023
A simple alert with logo image and color.

YMLogoAlert About YMLogoAlert A simple custom alert. YMLogoAlert lets you pop up a simple alert with a natural animation, your app's own color, font a

Youngminah 8 Dec 21, 2022
A simple custom popup dialog view for iOS written in Swift. Replaces UIAlertController alert style.

A simple custom popup dialog view for iOS written in Swift. Replaces UIAlertController alert style.

donggyu 5 Jan 26, 2022
(Experimental libraries) Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI.

UIPresentCoordinator Controls interrupt handling, such as alert views, and is compatible with UIKit and Swift UI. This library manages items that are

Yuki Tamazawa 1 Jan 22, 2022
Simple DropDown Alert View For Any iOS Projects.

⚠️ DEPRECATED, NO LONGER MAINTAINED JDropDownAlert JDropDownALert Simple DropDown Alert View For Any iOS Projects. Usage Top let alert = JDropDown

WonJo 71 Jul 17, 2022
Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.

SPAlert Popup from Apple Music & Feedback in AppStore. Contains Done, Heart, Error and other presets. Supports Dark Mode. I tried to recreate Apple's

Ivan Vorobei 1.8k Jan 7, 2023
BPStatusBarAlert is a library that allows you to easily make text-based alert that appear on the status bar and below navigation bar.

BPStatusBarAlert BPStatusBarAlert is a library that allows you to easily make text-based alert that appear on the status bar and below navigation bar.

Ben.Park 131 Aug 12, 2022
Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.

SPAlert Popup from Apple Music & Feedback in AppStore. Contains Done, Heart, Error and other presets. Supports Dark Mode. I tried to recreate Apple's

Ivan Vorobei 1.4k Dec 10, 2021
TextFieldAlert - A SwiftUI alert with text field(s) for iOS 13 and greater.

TextFieldAlert A SwiftUI alert with text field(s) for iOS 13 and greater. As Apple is going to introduce text field(s) as an alert actions in iOS 16,

Piotr Sochalewski 13 Dec 5, 2022
Beautiful animated Alert View. Written in Swift

SCLAlertView Animated Alert View written in Swift, which can be used as a UIAlertView or UIAlertController replacement. Since UIAlertView is deprecate

Viktor Radchenko 5.2k Jan 3, 2023
Live animated Alert View for iOS written in Swift

Sweet Alert iOS Beautiful Animated custom Alert View inspired from javascript library SweetAlert. Written in Swift this SweetAlertView can be used in

Sahil 2k Dec 22, 2022
A Swift package for iOS/tvOS for easy alert presentation

AlertPresenter Listed on the Swift Package Index and originally posted on my blog. It can be fiddly to handle the presentation of alerts on a specific

Chris Mash 14 Jul 1, 2022
Swift UI Kit to present clean modal/alert

CleanyModal is a good way to use UI-Customised alerts with ease Features Present some kind of clean alerts (With same API as UIAlertViewController) Ad

Lory Huz 487 Dec 2, 2022