Showing dropdown menu (popover) below BarButtonItem on click
Asked Answered
W

1

7

When I click "+" I want to show menu (popover) under the right BarButtonItem where will be two options. Pressing one of those options will lead to other view controllers.

I'm using Xcode 10 and Swift 4.2.

enter image description here

Like this: enter image description here

This is my code for now and nothing happen. What i do wrong? Can i write on different way?

import Foundation

class RootVC: UITableViewController {

    @IBOutlet weak var openSideMenu: UIBarButtonItem!

    let itemArray = ["1", "2", "3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        openSideMenu.target = self.revealViewController()
        openSideMenu.action = #selector(SWRevealViewController.revealToggle(_:))
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RootCell", for: indexPath)

        cell.textLabel?.text = itemArray[indexPath.row]

        return cell
    }

    @IBAction func addBarButtonPressed(_ sender: UIBarButtonItem) {
        let menu = UIMenuController.shared
        menu.menuItems =
            [UIMenuItem(title: "Test me", action: Selector("deleteLine")),
             UIMenuItem(title: "Test me", action: Selector("deleteLine")),
             UIMenuItem(title: "Test me", action: Selector("deleteLine"))]

        menu.setTargetRect((self.navigationItem.rightBarButtonItems?.first?.frame)!, in: self.view)
        becomeFirstResponder()
        menu.setMenuVisible(true, animated: true)

    }

}
extension UIBarButtonItem {

    var frame: CGRect? {
        guard let view = self.value(forKey: "view") as? UIView else {
            return nil
        }
        return view.frame
    }

}
Whichever answered 30/9, 2018 at 9:7 Comment(9)
can you show the menu controllerSecurity
@Tobi UIMenuController? Do not I have it, does it have to be in a separate class?Whichever
Please explain what you want to achieve - don’t make us watch a 5 minute video to find outBeesley
@AshleyMills i edit questionWhichever
In case you want to look for better resources on the component you want to use, they aren't called dropdown but popoversMew
@Mew I read that popovers are for development ipods app, not for iphoneWhichever
@Whichever You can present your modal view controllers in popovers on iPhones but you have to do a bit more work.Mew
@Mew do you have some link tutorial?Whichever
Here's a question that does what you want to achieve #39973479Mew
W
12

I solve my problem using AssistoLab/DropDown CocoaPods (link)

enter image description here

enter image description here

This is code:

import Foundation
import DropDown

class ViewController: UIViewController {

   @IBOutlet weak var addBarButton: UIBarButtonItem!

   let rightBarDropDown = DropDown()

   override func viewDidLoad() {
      super.viewDidLoad()

      rightBarDropDown.anchorView = addBarButton
      rightBarDropDown.dataSource = ["Generate New", "Add Manual"]
      rightBarDropDown.cellConfiguration = { (index, item) in return "\(item)" }
   }

   @IBAction func showBarButtonDropDown(_ sender: AnyObject) {

      rightBarDropDown.selectionAction = { (index: Int, item: String) in
        print("Selected item: \(item) at index: \(index)") }

      rightBarDropDown.width = 140
      rightBarDropDown.bottomOffset = CGPoint(x: 0, y:(rightBarDropDown.anchorView?.plainView.bounds.height)!)
      rightBarDropDown.show() 
   }
}
Whichever answered 1/10, 2018 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.