Install PopMenu using CocoaPods, add it to your Podfile:
PopMenu
was actually taken so let's call itNewPopMenu
'cause why not?
pod 'NewPopMenu'
Install PopMenu using Carthage, add it to your Cartfile:
github "CaliCastle/PopMenu"
- Xcode 8.0 +
- iOS 9.0 +
- Swift 4.0 +
Integrating PopMenu is extremely easy with a familiar workflow like presenting UIAlertController
with UIAlertAction
import NewPopMenu
There are 2 ways to present the menu in your view controller:
-
Use the default manager
-
Use the
PopMenuViewController
class directly
You can, however, choose either way to have the same result, whichever works best for you.
Use the default manager if you want quick setup and present menu seamlessly.
Use the PopMenuViewController
class directly if you want to have more control.
If you don't want to scroll, click the link to jump forward:
Basic Usage - Using Controller
The quickest way would be to use PopMenuManager
's default singleton with no additional setup required:
let manager = PopMenuManager.default
Add actions if you're using the manager:
manager.actions = [
PopMenuDefaultAction(title: "Action Title 1", image: UIImage(named: "icon"),
PopMenuDefaultAction(title: "Action Title 2", image: UIImage(named: "icon")
]
Or if you prefer the good ol' way to present a UIAlertController
with actionSheet
type, you can add each action like this:
let action1 = PopMenuDefaultAction(title: "Action Title 1", image: UIImage(named: "icon")
let action2 = PopMenuDefaultAction(title: "Action Title 2", image: UIImage(named: "icon")
manager.addAction(action1)
manager.addAction(action2)
That's how you set the actions for the menu.
Now all you have to do is to simply call present()
on the manager, either pass the view controller in the on
argument or PopMenuManager will automatically fetch the current top view controller to present the menu on top:
// Show menu without specifying which controller
manager.present()
// Or you can specify the controller to present on
manager.present(on: self)
Both should work just fine, but still, using manager.present(on: ...)
manually would be a safer way to go for presenting.
Manually initialize the controller:
let menuViewController = PopMenuViewController()
In order to know which action button is tapped, you'll need to comform to PopMenuViewControllerDelegate
protocol and then implement the method popMenuDidSelectItem(at index: Int)
in your view controller:
class ViewController: UIViewController {
func presentMenu() {
let manager = PopMenuManager.default
manager.popMenuDelegate = self
manager.present(on: self)
}
}
extension ViewController: PopMenuViewControllerDelegate {
func popMenuDidSelectItem(at index: Int) {
// Do stuff here...
}
}
Documentation is available at https://calicastle.github.io/PopMenu/