It is simple and easily customizable alert.
Can be used as UIAlertController
, it supports from iOS7.
- Swift 3.0
- iOS 7.0 or later
Add the following to your Podfile
:
pod "SimpleAlert"
use_frameworks!
Note: the use_frameworks!
is required for pods made in Swift.
Add the following to your Cartfile
:
github "KyoheiG3/SimpleAlert"
Just add everything in the source file to your project.
- Add
SimpleAlert
namespace for iOS7
If target is ios8.0 or later, please import the SimpleAlert
.
import SimpleAlert
View simple Alert
let alert = AlertController(title: "title", message: "message", style: .Alert)
alert.addTextFieldWithConfigurationHandler()
alert.addAction(AlertAction(title: "Cancel", style: .Cancel))
alert.addAction(AlertAction(title: "OK", style: .OK))
presentViewController(alert, animated: true, completion: nil)
Customize default contents
let alert = AlertController(title: "title", message: "message", style: .Alert)
alert.addTextFieldWithConfigurationHandler() { textField in
textField.frame.size.height = 33
textField.backgroundColor = nil
textField.layer.borderColor = nil
textField.layer.borderWidth = 0
}
alert.configContentView = { [weak self] view in
if let view = view as? AlertContentView {
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))
presentViewController(alert, animated: true, completion: nil)
Rounded button Alert View
let alert = AlertController(view: UIView(), style: .Alert)
let action = AlertAction(title: "?", style: .Cancel)
alert.addAction(action)
action.button.frame.size.height = 144
action.button.titleLabel?.font = UIFont.boldSystemFontOfSize(96)
action.button.setTitleColor(UIColor.redColor(), forState: .Normal)
alert.configContainerWidth = {
return 144
}
alert.configContainerCornerRadius = {
return 72
}
alert.configContentView = { view in
view.backgroundColor = UIColor.whiteColor()
}
presentViewController(alert, animated: true, completion: nil)
More customizable if you create a subclass
class AlertController: AlertController {
override func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)? = nil) {
super.addTextFieldWithConfigurationHandler() { textField in
textField.frame.size.height = 33
textField.backgroundColor = nil
textField.layer.borderColor = nil
textField.layer.borderWidth = 0
configurationHandler?(textField)
}
}
override func configureButton(style :AlertAction.Style, forButton button: UIButton) {
super.configureButton(style, forButton: button)
if let font = button.titleLabel?.font {
switch style {
case .OK:
button.titleLabel?.font = UIFont.boldSystemFontOfSize(20)
button.setTitleColor(UIColor.grayColor(), forState: .Normal)
case .Cancel:
button.backgroundColor = UIColor.darkGrayColor()
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
case .Default:
button.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
default:
break
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configContentView = { [weak self] view in
if let view = view as? AlertContentView {
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
}
}
}
}
- Default
- OK
- Cancel
- Destructive
init(title: String, style: AlertAction.Style, handler: ((AlertAction!) -> Void)?)
- Set title and style, can add button.
- Set button action handler.
var enabled: Bool
- Set button enabled.
var button: UIButton!
- Can get a button.
- Can get after button has been added to the
Controller
.
backgroundColor
of ContentView
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
Controller.Style
is in the case ofActionSheet
does not appear.
- Alert
- ActionSheet
init(title:String?, message:String?, style: AlertController.Style)
- Show default view.
init(view: UIView?, style: Controller.Style)
- Show custom view.
- Automatically set the width.
func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)?)
- Add Text Field, and set handler.
Controller.Style
is in the case ofActionSheet
does not add.
func addAction(action: AlertAction)
- Add action button.
var configContainerWidth: (() -> CGFloat?)?
- Change view width.
var configContainerCornerRadius: (() -> CGFloat?)?
- Change view corner radius.
var configContentView: ((UIView!) -> Void)?
- Customize contents.
- By default,
UIView
argument isContentView
.
- 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
.
Under the MIT license. See LICENSE file for details.