A powerful customizable Alert View library written in Swift.
SwiftAlertView is the best alternative for UIKit's UIAlertView and UIAlertController. With SwiftAlertView, you can easily make your desired Alert View in some lines of code.
Just add the following line in to your pod file:
pod 'SwiftAlertView', '~> 1.1.0'
Use version
1.2.0
For compatible with Swift 2.0. Download version 1.2.0 from Releases.
Drag and drop the file named SwiftAlertView
in your project and you are done.
- Initialize the alert view with a custom view.
- Initialize the alert view with a xib file.
- Closure and callbacks for handling button touched events.
- Change the alert appearance: background color or background image, border radius.
- Change the title appearance: font, color, margin, spacing, visibility.
- Change the message appearance: font, color, margin, spacing, visibility.
- Change the button appearance: font, color.
- Change the separator appearance: color, visibility.
- Change the alert appear behaviour and disappear behaviour.
- APIs are exactly same as UIAlertView.
- And many more ...
// Initialize with title and message
let alertView = SwiftAlertView(title: "Sample Title", message: "Sample Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Button 1", "Button 2", "Button 3")
// Initialize with a custom view
let alertView = SwiftAlertView(contentView: customView, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
// Initialize with nib name
let alertView = SwiftAlertView(nibName: "CustomView", delegate: self, cancelButtonTitle: "I love this feature")
// Show at center of screen
alertView.show()
// Show at center of a view
alertView.show(view)
// Programmatically dismiss the alert view
alertView.dismiss()
SwiftAlertView.show(title: "Lorem ipsum", message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: ["OK"], configureAppearance: { (alertView: SwiftAlertView) -> (Void) in
// customize alert view appearance here
alertView.backgroundColor = UIColor ( red: 0.8733, green: 0.5841, blue: 0.909, alpha: 1.0 )
}, clickedButtonAction: { (buttonIndex) -> (Void) in
print("Button Clicked At Index \(buttonIndex)\n")
})
alertView.clickedButtonAction = {(buttonIndex) -> Void in
println("Button Clicked At Index \(buttonIndex)")
}
alertView.clickedCancelButtonAction = {
println("Cancel Button Clicked")
}
alertView.clickedOtherButtonAction = {(buttonIndex) -> Void in
println("Other Button Clicked At Index \(buttonIndex)")
}
If you don't want to use closures, make your view controller conform SwiftAlertViewDelegate
and use delegate methods:
func alertView(alertView: SwiftAlertView, clickedButtonAtIndex buttonIndex: Int) {
println("Button Clicked At Index \(buttonIndex)")
}
func didPresentAlertView(alertView: SwiftAlertView) {
println("Did Present Alert View")
}
func didDismissAlertView(alertView: SwiftAlertView) {
println("Did Dismiss Alert View")
}
SwiftAlertView can be customized with the following properties:
var titleLabel: UILabel! // access titleLabel to customize the title font, color
var messageLabel: UILabel! // access messageLabel to customize the message font, color
var cancelButtonIndex: Int! // default is 0, set this property if you want to change the position of cancel button
var backgroundImage: UIImage?
// var backgroundColor: UIColor? // inherits from UIView
var buttonTitleColor: UIColor! // to change the title color of all buttons
var buttonHeight: Double! // default is 44
var separatorColor: UIColor! // to change the separator color
var hideSeparator: Bool! // to hide the separater color
var cornerRadius: Double! // default is 8 px
var dismissOnOtherButtonClicked: Bool! // default is true, if you want the alert view will not be dismissed when clicking on other buttons, set this property to false
var highlightOnButtonClicked: Bool! // default is true
var dimBackgroundWhenShowing: Bool! // default is true
var dimAlpha: Double! // default is 0.2
var dismissOnOutsideClicked: Bool! // default is false
var appearTime: Double! // default is 0.2 second
var disappearTime: Double! // default is 0.1 second
var appearType: SwiftAlertViewAppearType! // to change the appear type
var disappearType: SwiftAlertViewDisappearType! // to change the disappear type
// customize the margin & spacing of title & message
var titleSideMargin: Double! // default is 20 px
var messageSideMargin: Double! // default is 20 px
var titleTopMargin: Double! // default is 20 px
var messageBottomMargin: Double! // default is 20 px
var titleToMessageSpacing: Double! // default is 10 px
// closure for handling button clicked action
var clickedButtonAction:((buttonIndex: Int) -> (Void))? // all buttons
var clickedCancelButtonAction:((Void) -> (Void))? // for cancel button
var clickedOtherButtonAction:((buttonIndex: Int) -> (Void))? // sometimes you want to handle the other button click event but don't want to write if/else in clickedButtonAction closure, use this property
// access the buttons to customize their font & color
func buttonAtIndex(index: Int) -> UIButton?
Contributions for bug fixing or improvements are welcomed. Feel free to submit a pull request. If you have any questions, feature suggestions or bug reports, please send me an email to [email protected].