Skip to content

jacks205/JModalController

Repository files navigation

JModalController

CI Status Version License Platform

An easy and awesome way to make custom modals.

⚠️ For Swift 2.3 usage, use 0.1.x releases ⚠️

⚠️ For Swift 3.0 usage, use 0.2.x or later releases ⚠️

⚠️ This Readme uses the Swift 3.0 release, look to previous Readmes for older API ⚠️

JModalController

Run Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

JModalController requires iOS 8 or greater to use.

Installation

JModalController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'JModalController'

Usage

Create a ViewController like you would any other storyboard controller.

Create ViewController in storyboard

Size the controller to whatever your needs are. The width and height both can be shorter than the presenting view controller.

Size the modal

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.

Uncheck Resize View From NIB

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 a basePresentingViewController. 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, use navigationController/tabBarController instead of self

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

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]

JModalDelegate

Importing the pod automatically makes UIViewControllers conform to JModalDelegate. Overriding dismissModal will allow getting data back from the modal.

TODOs

  • Tests
  • More customizability
  • Rx version

Contribute

Anyone is welcome to help with the improvement of JModalController through finding issues or any pull requests.

Author

Mark Jackson, [email protected]

License

JModalController is available under the MIT license. See the LICENSE file for more info.