It is simple and easily customizable alert.
Can be used as UIAlertController.
- Swift 5.0
- iOS 9.0 or later
Add the following to your Podfile:
pod"SimpleAlert"Add the following to your Cartfile:
github"KyoheiG3/SimpleAlert"View simple Alert
letalert=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
letalert=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
letalert=AlertController(view:UIView(), style:.alert)
alert.contentWidth =144
alert.contentCornerRadius =72
alert.contentColor =.white
letaction=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
classCustomAlertController:AlertController{overridefunc addTextField(configurationHandler:((UITextField)->Void)?=nil){
super.addTextField{ textField in
textField.frame.size.height =33
textField.backgroundColor =nil
textField.layer.borderColor =nil
textField.layer.borderWidth =0configurationHandler?(textField)}}overridefunc 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}}overridefunc 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}}- default
- ok
- cancel
- destructive
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.
varisEnabled:Bool- Set button enabled.
letbutton:UIButton- Can get a button.
- Can get after button has been added to the
AlertController.
backgroundColor of AlertContentView will be reflected in the overall backgroundColor.
varbaseView:UIView!- Base view for contents
vartitleLabel:UILabel!- Title label
varmessageLabel:UILabel!- Message Label
vartextBackgroundView:UIView!- Base view for Text Field
UIAlertControllerStyleis in the case ofactionSheetdoes not appear.
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.
openvarcontentWidth:CGFloatopenvarcontentColor:UIColor?openvarcontentCornerRadius:CGFloat?openvarcoverColor:UIColoropenvarmessage:String?- Can change alert style.
publicprivate(set)varactions:[SimpleAlert.AlertAction]publicvartextFields:[UITextField]{get}- Can get actions and text fields that is added.
func addTextField(configurationHandler:((UITextField)->Swift.Void)?= default)- Add Text Field, and set handler.
UIAlertControllerStyleis in the case ofactionSheetdoes 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.
- 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.
Follow me 🎉
Under the MIT license. See LICENSE file for details.



