Search
  • John N Blanchard

Swift iOS: JBSnackBar

Many times I find myself working in a crowded project. And often enough on top of that, constrained by time. When I am facing storyboard dependencies and tight deadlines, I turn to a singleton implementation. With this pattern we ensure there is one provider for our feature, and it's global. In Swift it is really easy to make a singleton.


Today I worked on an iOS component, that uses the singleton pattern. My goal was to create a controller I can put in my apps that mimics Android Snackbar. This controller doesn't interact with an active view controller, instead it asks the application for its top view controller then displays some content view.



In the past I have created singletons that handle photo libraries or facial recognition. There are cool things we can do with a singleton, like saving an operation upon application state change. Swift-app singletons really shines with http calls. We can use the singleton to handle service failure and retries.

JBSnackBarManager is responsible for creating, animating and handling the bottom information bar. All that an app has to do is call a show convienence method or show with a content view. And the bar will bounce upward.

The two show calls above are convenience methods, the manager will create the content view for you using the text and detailImage parameters. But for custom bar solutions, use the show method to set your own content view.

Above is how I show one of the bottom bars in an example app. This solution is a vertical stack with a label and image. Note it is also a really big bottom view. My manager only resizes the bar while it is out of view of the user. The controller is best at changing its content and animating across all size classes and devices.


22 views