An easy and awesome way to make custom modals.
To run the example project, clone the repo, and run pod install
from the Example directory first.
JModalController requires iOS 8 or greater to use.
JModalController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'JModalController'
Create a ViewController like you would any other storyboard controller.
Size the controller to whatever your needs are. The width and height both can be shorter than the presenting view controller.
Note: Make sure to uncheck 'Resize View From NIB' or the modal won't be your custom size. If this isn't unchecked, then you much manually set the frame before presenting the modal.
Using a Storyboard, you just need to load in the controller and call presentModal()
#import JModalController
// ...
func showModal() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let simpleVC = storyboard.instantiateViewControllerWithIdentifier("SimpleModalViewController") as? SimpleModalViewController
//Set the delegate in order to dismiss the modal
simpleVC?.delegate = self
//Set configuration settings to customize how the modal presents
let config = JModalConfig(animationDuration: 0.2, tapOverlayDismiss: true, transitionDirection: .bottom, backgroundTransform: true)
//Present the modal!
//`self` if no navigation or tabBar controllers are present!
presentModal(self, modalViewController: simpleVC!, config: config) {
print("Presented Simple Modal")
}
}
Note:
presentModal
requires abasePresentingViewController
. This controller should be the base controller you wish to perform the transform on. So if you have your controller embedded in an UINavigationController and/or UITabBarController, usenavigationController
/tabBarController
instead ofself
Dismissing the modal is easy, just call delegate.dismissModal(self, nil)
import JModalController
var delegate : JModalDelegate?
// ...
@IBAction func dismiss(sender: AnyObject) {
//Dismiss Modal, give the data parameter anything you want to send back to the presenting view controller.
self.delegate!.dismissModal(self, data: nil)
}
JModalConfig is a class that allows customization of the modal. Attributes describe length of animation, color of background animation, and much more.
Here are the attributes that can be customized. Attributes may be set through the initializer or manually. To see what these do, checkout the demo project.
/// Background color of the overlay that covers your current view controller. Default set to UIColor(white: 0, alpha: 0.3)
public var overlayBackgroundColor: UIColor
/// Direction where you want the modal to appear from. Default set to .bottom
public var transitionDirection: JModalTransitionDirection
/// View animation options for how to have modal and overlay to appear. Default set to .curveLinear
public var animationOptions: UIViewAnimationOptions
/// Duration on how long views animate on appearing and dismissing. Default set to 0.3
public var animationDuration: NSTimeInterval
/// The 0 - 1 value of how much the background viewcontroller transforms on modal appearance. Default set to 0.93
public var backgroundTransformPercentage: Double
/// Boolean indicating of background viewcontroller should transform on modal appearance. Default set to true
public var backgroundTransform: Bool
/// Boolean indicating if tapping on the background overlay should dismiss modal. Default set to true
public var tapOverlayDismiss: Bool
///Swipe gesture directions you want your modal to support for dismissing. Default set to []
public var swipeDirections: [UISwipeGestureRecognizerDirection]
Importing the pod automatically makes UIViewController
s conform to JModalDelegate. Overriding dismissModal
will allow getting data back from the modal.
- Tests
- More customizability
- Rx version
Anyone is welcome to help with the improvement of JModalController through finding issues or any pull requests.
Mark Jackson, [email protected]
JModalController is available under the MIT license. See the LICENSE file for more info.