KyoheiG3 / SimpleAlert

Customizable simple Alert and simple ActionSheet for Swift
MIT License
398 stars 45 forks source link
customizable ios simplealert swift

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

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

Initialize

init(title: String, style: SimpleAlert.AlertAction.Style, dismissesAlert: Bool = default, handler: ((SimpleAlert.AlertAction?) -> Swift.Void)? = default)

Variable

var isEnabled: Bool
let button: UIButton

AlertContentView

backgroundColor of AlertContentView will be reflected in the overall backgroundColor.

var baseView: UIView!
var titleLabel: UILabel!
var messageLabel: UILabel!
var textBackgroundView: UIView!

AlertController

Initialize

init(title: String?, message: String?, style: UIAlertControllerStyle)
init(title: String? = default, message: String? = default, view: UIView?, style: UIAlertControllerStyle)

Variable

open var contentWidth: CGFloat
open var contentColor: UIColor?
open var contentCornerRadius: CGFloat?
open var coverColor: UIColor
open var message: String?
public private(set) var actions: [SimpleAlert.AlertAction]
public var textFields: [UITextField] { get }

Function

func addTextField(configurationHandler: ((UITextField) -> Swift.Void)? = default)
func addAction(_ action: SimpleAlert.AlertAction)
func configureActionButton(_ button: UIButton, at style: SimpleAlert.AlertAction.Style)
func configureContentView(_ contentView: SimpleAlert.AlertContentView)

The difference between default UIAlertController

Author

Kyohei Ito

Follow me 🎉

LICENSE

Under the MIT license. See LICENSE file for details.