SnackView
An easy way to present customizable bottom-half alert.
What's new
What's new in 1.0.9
-
New SnackViewDataSource protocol with which create your SnackView.
-
New SVImageViewItem class with which display images.
-
Dark mode is not compatible yet, for now SnackView continue to show light UI.
-
Compatible with the new UIWindowSceneDelegate system.
What's new in 1.0.8
- Fixed crash that occurs when SnackView was init with an empty SVItem array.
Installation
CocoaPods
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like SnackView in your projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.0.1+ is required to build SnackView 1+ (along with Swift 3 and Xcode 9).
Podfile
- Swift 3.x: >= 1.0.1 Download here.
To integrate SnackView into your Xcode project using CocoaPods, specify it in your
Podfile
:
use_frameworks!
pod 'SnackView', '~> 1.0.9'
Then, run the following command:
$ pod install
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SnackView into your Xcode project using Carthage, specify it in your Cartfile
:
github "lucacasula91/SnackView"
SnackView 1.0.9 is not available yet on Carthage.
Manual installation
If you want to install SnackView manually, just drag SnackView Library folder into your project.
Usage
Create your custom SnackView alert
SnackView includes some default UI elements as Button, Label and other complex UI. The first step is to create an array of SVItem. SVItem is the class of every element that SnackView can include within it.
Here an example of simple SnackView alert:
class MyCustomClass: UIViewController {
override func viewDidLoad() { }
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Present the SnackView
let dataSource = self
SnackView(with: dataSource).show()
}
}
// MARK: - SnackViewDataSource Section
extension MyCustomClass: SnackViewDataSource {
func titleFor(snackView: SnackView) -> String {
return "What's New"
}
func cancelTitleFor(snackView: SnackView) -> String? {
return "Dismiss"
}
func itemsFor(snackView: SnackView) -> [SVItem] {
let descriptionItem = SVDescriptionItem(withDescription: "In this last release of SnackView we...")
let imageItem = SVImageViewItem(withImage: UIImage(named: "hat_is_new")!,
andContentMode: .scaleAspectFill)
return [descriptionItem, imageItem]
}
}
Deprecated methods
With SnackView 1.0.9 the current method are now deprecated:
- init(withTitleOptions: andItems:)
- init(withTitle: andCloseButtonTitle: andItems:)
SnackViewDataSource contains all you need to handle title, cancel button title and items to display. For more info please see the SnackViewDataSource protocol documentation code.
- func insert(item: atIndex:)
- func remove(item:)
- func removeItemAt(index:)
- func updateWith(items:)
Please start using the new SnackViewDataSource instead, all the above operations can be performed simply by changing the return content in the func itemsFor(snackView:)
method of SnackViewDataSource and call the reloadData()
function to perform an update.
SVItems included
SnackView provides some SVItems ready to use, here below the list of SVItems available:
SVApplicationItem
SVApplicationItem(withIcon: UIImage(named: "AppIcon"),
withTitle: "Ipsum lorem",
andDescription: "Lorem ipsum dolor sit amet...")
SVDescriptionItem
SVDescriptionItem(withDescription: "Lorem ipsum dolor sit amet...")
SVTextFieldItem
SVTextFieldItem(withPlaceholder: "Create Password",
isSecureField: true)
SVDetailTextItem
SVDetailTextItem(withTitle: "Elit amet",
andContent: "Lorem ipsum dolor sit amet...")
SVButtonItem
SVButtonItem(withTitle: "Continue") { /* Button action here */ }
SVSwitchItem
SVSwitchItem(withTitle: "Push Notifications",
andContent: "Activate to stay up to date...") { (isOn) in /* Switch action here */ }
SVLoaderItem
SVLoadingItem(withSize: .large,
andText: "Lorem ipsum dolor sit amet...")
SVImaveViewItem
SVImageViewItem(with: UIImage(named: "hat_is_new")!,
andContentMode: .scaleAspectFill)
Create custom SVItems
You can create custom items subclassing SVItem class.
Here below an example.
import UIKit
import SnackView
//Create a subclass of SVItem
class SVCustomItem: SVItem {
//Pass all parameters in init method to customize your SVItem
init(withColor color: UIColor) {
super.init()
//Add an UIView
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = color
self.addSubview(customView)
//Add UIView contraints
let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-[customView(70)]-|", options: [], metrics: nil, views: ["customView":customView])
let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[customView]-|", options: [], metrics: nil, views: ["customView": customView])
self.addConstraints(vConstraints + hConstraints)
}
required public convenience init?(coder aDecoder: NSCoder) {
self.init(coder: aDecoder)
}
}
Contributing
If you want to contribute to make SnackView a better framework, submit a pull request.
Please consider to open an issue for the following reasons:
- If you have questions or if you need help using SnackView
- If you found a bug
- If you have some feature request