Rajeev
Rajeev

Reputation: 5002

Show menu controller on top of cell in iOS collection view

I want to show a UIMenuController with custom actions when the user long pressed on a cell in the collection view which is embedded in container view.

I have implemented as mentioned in this stack overflow solution which is working fine. Copy Callout in UICollectionView

The problem I have now is menu items are coming below the cell even though I kept the arrow direction to UIMenuControllerArrowDown.

enter image description here

My collection view is part of a container view which is inside a view controller. When I press on an image in the collection view, it is showing the menu item on the bottom of the cell. How can I show it on the top?

I tried to show it in a particular view as below, but my effort got no use.

[menu setTargetRect:cell.bounds inView:parentControllerView];

Can someone suggest how I can resolve this problem? Let me know if my question is not clear or needs more details.

Upvotes: 2

Views: 871

Answers (2)

Devang Tandel
Devang Tandel

Reputation: 3008

Your menu is showing bottom of the cell as you have set the cell's parent view to display menu, the parent view's height is not capable to display your menu below your cell, so it is displaying overlapping your cell. So if you want to show your menu below your cell not no it you have to change the view in which menu is showing..set

[menu setTargetRect:cell.bounds inView:[UIApplication sharedApplication] window];

this will display your menu in main window of application.

Upvotes: 2

Jordan H
Jordan H

Reputation: 55825

The problem arises when you try to present the menu above a view that would result in the menu appearing overtop the navigation bar. iOS doesn't want to show it overtop the nav bar, and instead will push it down. If your items were lower on the screen, it would properly appear above the view and point down to it as expected. It may be worth filing a bug report at bugreport.apple.com to request the menu appear overtop the navigation bar.

Also, it is good to use the cell's bounds as the target rect, and the cell for the view as opposed to the parent controller's view, like so:

[menu setTargetRect:cell.bounds inView:cell];

Now, I have found a workaround to force the menu to appear over the navigation bar. Instead of providing the cell's bounds as the target rect, push it down a little bit yourself. For some reason, doing this will prevent iOS from pushing it down when it would appear over the navigation bar.

[menu setTargetRect:CGRectMake(0, 10, cell.bounds.width, cell.bounds.height - 10) inView:cell];

Upvotes: 0

Related Questions