Solutions that UIMenuController cannot display within Cell (iOS9.2)

  • 2020-06-23 01:59:37
  • OfStack

Xcode7.2, iOS9.2

Try adding the LongPress gesture inside CollectionViewCell to show UIMenuController.


@implementation CollectionViewCell // Inherited from UICollectionViewCell

 

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
 longPress.minimumPressDuration = 1;
 [self addGestureRecognizer:longPress];// Add a long press gesture 
// Implement the long press method 
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
 switch (gesture.state) {
 case UIGestureRecognizerStateBegan:
 {

  [self becomeFirstResponder];<span style="color: rgb(255, 0, 0);">// Must have! </span>
  UIMenuController *menu = [UIMenuController sharedMenuController];
  UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@" save " action:@selector(savePic)];
  menu.menuItems = [NSArray arrayWithObject:item1];
  [menu setTargetRect:self.scView.frame inView:self.scView.superview];
  [menu setMenuVisible:YES animated:YES];
 }

  break;
 default:
  break;
 }

}


<span style="color: rgb(0, 128, 0);">// Must be achieved. Note that not - (BOOL)becomeFirstResponder methods </span>
- (BOOL)canBecomeFirstResponder
{
 return YES;
} 

Implement the above code to display UIMenuController when you long press cell. But one problem is that when I slide CollectionView, menu doesn't have time 1 hidden.
So, you can hide inside the method where you need menu. Implement the following code.
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:NO];


Related articles: