A Swift based implementation of the Android Snackbar for iOS
TTGSnackbar is useful for showing a brief message at bottom or top of the screen with one or two action buttons.
It appears above all other elements on screen.
It disappears after a timeout or after user click the action button.
Swift 3
Xcode 8
iOS 8+
You can use CocoaPods to install TTGSnackbar
by adding it to your Podfile
:
pod "TTGSnackbar"
You can use Carthage to install TTGSnackbar
by adding it to your Cartfile
:
github "zekunyan/TTGSnackbar"
And you need to import the module.
import TTGSnackbar
let snackbar = TTGSnackbar(message: "TTGSnackbar !", duration: .short)
snackbar.show()
let snackbar = TTGSnackbar(message: "TTGSnackBar !",
duration: .middle,
actionText: "Action!",
actionBlock: { (snackbar) in
print("Click action!")
})
snackbar.show()
let snackbar = TTGSnackbar(message: "TTGSnackbar !",
duration: .forever,
actionText: "Action",
actionBlock: { (snackbar) in
print("Click action!")
// Dismiss manually after 3 seconds
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {
snackbar.dismiss()
}
})
snackbar.show()
let snackbar = TTGSnackbar(message: "Two actions !", duration: .long)
// Action 1
snackbar.actionText = "Yes"
snackbar.actionTextColor = UIColor.green
snackbar.actionBlock = { (snackbar) in NSLog("Click Yes !") }
// Action 2
snackbar.secondActionText = "No"
snackbar.secondActionTextColor = UIColor.yellow
snackbar.secondActionBlock = { (snackbar) in NSLog("Click No !") }
snackbar.show()
let snackbar = TTGSnackbar(message: "TTGSnackbar !", duration: .long)
// Add icon image
snackbar.icon = UIImage(named: "emoji_cool_small")
snackbar.show()
let snackbar = TTGSnackbar(message: "", duration: .long)
// Get custom content view
let customContentView = UINib(nibName: "CustomView", bundle:Bundle.main).instantiate(withOwner: nil, options: nil).first as! UIView?
// Set custom content view
snackbar.customContentView = customContentView
snackbar.show()
message: String
defines the message to display. Supports multi line text.
messageTextColor: UIColor
defines the message text color.
messageTextFont: UIFont
defines the message text font.
duration: TTGSnackbarDuration
defines the display duration.
TTGSnackbarDuration
: short
, middle
, long
and forever
.
When you set forever
, the snackbar will show an activity indicator after user click the action button and you must dismiss the snackbar manually.
actionText: String
defines the action button title.
actionTextColor: UIColor
defines the action button title color.
actionTextFont: UIFont
defines the action button title font.
actionMaxWidth: CGFloat
defines the action button max width. Min is 44.
actionTextNumberOfLines: Int
defines the number of lines of action button title. Default is 1.
actionBlock: TTGActionBlock?
will be called when user clicks the action button.
// TTGActionBlock definition.
public typealias TTGActionBlock = (snackbar: TTGSnackbar) -> Void
secondActionText: String
secondActionTextColor: UIColor
secondActionTextFont: UIFont
secondActionBlock: TTGActionBlock?
dismissBlock: TTGDismissBlock?
will be called when snackbar dismiss automatically or when user click action button to dismiss the snackbar.
// TTGDismissBlock definition.
public typealias TTGDismissBlock = (snackbar: TTGSnackbar) -> Void
animationType: TTGSnackbarAnimationType
defines the style of snackbar when it show and dismiss.
TTGSnackbarAnimationType
: fadeInFadeOut
, slideFromBottomToTop
, slideFromBottomBackToBottom
, slideFromLeftToRight
, slideFromRightToLeft
, slideFromTopToBottom
and slideFromTopBackToTop
.
The default value of animationType
is slideFromBottomBackToBottom
, which is the same as Snackbar in Android.
animationDuration: NSTimeInterval
defines the duration of show and hide animation.
leftMargin: CGFloat
, rightMargin: CGFloat
, topMargin: CGFloat
and bottomMargin: CGFloat
defines the margins of snackbar
contentInset: UIEdgeInsets
defines the padding(content inset) of content in the snackbar. Default is UIEdgeInsets.init(top: 0, left: 4, bottom: 0, right: 4)
.
cornerRadius: CGFloat
defines the corner radius of snackbar.
icon: UIImage
defines the icon image.
iconContentMode: UIViewContentMode
defines the content mode of icon imageView.
containerView: UIView
defines the custom container(super) view for snackbar to show.
customContentView: UIView?
defines the custom content view to show in the snackbar.
separateViewBackgroundColor: UIColor = UIColor.gray
defines the separator line color.
activityIndicatorViewStyle: UIActivityIndicatorViewStyle
defines the activityIndicatorViewStyle in snackbar.
activityIndicatorViewColor: UIColor
defines the activityIndicatorView color in snackbar.
animationSpringWithDamping: CGFloat
defines the spring animation damping value. Default is 0.7.
animationInitialSpringVelocity: CGFloat
defines the spring animation initial velocity. Default is 5.