blackmirror-media / BMPickerModal

BMPickerModal is a control showing a UIPicker or a UIDatePicker in a modal view.
MIT License
5 stars 3 forks source link

BMPickerModal

BMPickerModal is an iOS drop-in class that displays a UIPicker or a UIDatePicker as modal view or in a popover controller on the iPad. Used to let the user select from a list of data or pick a date without leaving the current screen. Closures allow easy customisation.

Requirements

Built in Swift 4 for iOS 10.0+. All devices supported. can be used in both Swift and in ObjectiveC projects.

Adding BMPickerModal To Your Project

Cocoapods

CocoaPods is the recommended way to add BMPickerModal to your project. As BMPickerModal is written in Swift, you need to add the use_frameworks! option to your podfile.

pod 'BMPickerModal'

Usage

Import the module to your project.

import BMPickerModal

Creating

Create an optional stored property for your BMPickerModal. Then instantiate the control, when the your view is already added to the window. This is needed for the control to be added properly.

class SomeClass {
  var datePickerModal: BMPickerModal?

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.datePickerModal = BMPickerModal()
    self.datePickerModal?.mode = .datePicker

    self.datePickerModal?.show { selectedDate in
      print(selectedDate)
      // Do something with the date here
    }
  }
}

Available modes:

Showing On The iPhone

datePickerModal?.show { selectedDate in
  print(selectedDate)
  // Do something with the date here
}

Checking whether the control is visible

let visible: Bool = self.datePickerModal?.isVisible

Showing On The iPad

selection: Closure to be executed when date/data is selected sourceView: View to show from sourceRect: CGRect to align to inViewController: ViewController used to present the modal

self.datePickerModal?
  .showInPopover({ (selectedDate) -> Void in
    let theNewDate = selectedDate as! NSDate
  },
  sourceView: self.view,
  sourceRect: cell!.frame,
  inViewController: self)

Checking whether the control is shown in a popover

let inPopover: Bool = self.datePickerModal.shownInPopover

Dismissing

self.datePickerModal?.dismiss()

You can add custom actions to the dismissal event by defining the onDismiss closure.

self.datePickerModal?.onDismiss = {
  // do stuff when dismissed
}

Customising the DatePicker

Access the UIDatePicker view and cusomise as per the Apple documentation.

self.datePickerModal?.datePicker.datePickerMode = UIDatePickerMode.date

Customising the Picker

Set the pickerDataSource NSArray property for filling the UIPickerView.