CaliCastle / PopMenu

A fully customizable popup style menu for iOS 😎
https://popmenu.cali.so
MIT License
1.6k stars 182 forks source link

How Can I set the menu offset the sourceView #14

Open deepindo opened 5 years ago

deepindo commented 5 years ago

✔️ Issue Checklist

✍🏻 Issue Description

How can I let the menu below the action button when the menu display. Now it have cover the button. Can you suggest some method? Thx!

💻 Environment

CaliCastle commented 5 years ago

I don't see an easy way to workaround it just yet, that should be a nice feature to have.

I'll add this to the todo list, thanks!

kfound commented 5 years ago

I'd also like this feature. Ideally it would display a bit more like a popup menu with a small arrow pointing to the source button. Right now the PopMenu feels very disconnected from the origin button when using sourceView. Thanks!

XRayAdamo commented 5 years ago

Excellent idea. I am still looking for good popup menu but I have to use it with UICollectionViewCell and would like popup to be shown near the cell. Probably adding something like "source" to show near that view will be great!

raxityo commented 5 years ago

Slightly unrelated, but here's an extension I use to show pop-up as a result of a gesture on a view (For example: long press on a UICollectionViewCell):

extension PopMenuManager {
    func present(with gesture: UIGestureRecognizer, on viewController: UIViewController, animated: Bool = true, completion: (() -> UIView?)? = nil) {
        let sourceView = UIView(frame: CGRect(origin: gesture.location(in: nil), size: .zero))
        UIApplication.shared.keyWindow?.addSubview(sourceView)
        present(sourceView: sourceView, on: viewController, animated: animated) {
            sourceView.removeFromSuperview()
        }
    }
}

Similar technique can be used to translate the source frame. Having an option to provide source point or frame would be sweet of course 👍

CaliCastle commented 5 years ago

@raxityo thank you for providing this great example of an extension for PopMenuManager, yeah these things are definitely coming in the next update, I'll be working on these improvements soon and thanks again for the amazing feedback and support!

zhongxinghong commented 4 years ago

I'd like to share may solution too !

extension PopMenuManager {

    public func present(
        navItem: UINavigationItem,  // navigationItem.rightBarButtonItem
        on viewController: UIViewController? = nil,
        animated: Bool = true,
        completion: (() -> Void)? = nil) {

        guard let button = navItem.value(forKey: "view") as? UIView else {
            present(sourceView: nil, on: viewController, animated: animated, completion: completion)
            return
        }

        let absFrame = button.convert(button.frame, to: nil)
        let newOrigin = CGPoint(x: absFrame.origin.x, y: absFrame.origin.y + absFrame.height)

        let sourceView = UIView(frame: CGRect(origin: newOrigin, size: .zero))
        UIApplication.shared.keyWindow?.addSubview(sourceView)

        present(sourceView: sourceView, on: viewController, animated: animated) {
            sourceView.removeFromSuperview()
            completion?()
        }
    }
}

The will allow the menu appear below the button of navigationItem.rightBarButtonItem :)

omarojo commented 3 years ago

Setting an Offset would be very very useful.

m7mdra commented 3 years ago

Slightly unrelated, but here's an extension I use to show pop-up as a result of a gesture on a view (For example: long press on a UICollectionViewCell):

extension PopMenuManager {
    func present(with gesture: UIGestureRecognizer, on viewController: UIViewController, animated: Bool = true, completion: (() -> UIView?)? = nil) {
        let sourceView = UIView(frame: CGRect(origin: gesture.location(in: nil), size: .zero))
        UIApplication.shared.keyWindow?.addSubview(sourceView)
        present(sourceView: sourceView, on: viewController, animated: animated) {
            sourceView.removeFromSuperview()
        }
    }
}

Similar technique can be used to translate the source frame. Having an option to provide source point or frame would be sweet of course 👍

Thank you very much , i managed to show menu at location where user taps by modify your answer a bit, i will share it just incase

             // let location = (location from guesture)

                let sourceView = UIView(frame: CGRect(origin: location, size: .zero))

                view.addSubview(sourceView)

                manager.present(sourceView: sourceView, on: self, animated: true) {

                    sourceView.removeFromSuperview()

                }
            }