Quick actions inside the app like iOS files app

428 views Asked by At

I want to implement quick actions like native Files application, on long press any item quick action view appears. Is there anything available in iOS to achieve it. I have attached some screenshots for better understanding. I know about Quick actions on home screen, I only want to achieve such thing inside my App not on AppIcon tap.

iOS Files App

long press on image/file long press on folder

1

There are 1 answers

0
Arasuvel On

It's called a context menu. See https://developer.apple.com/documentation/uikit/uicontrol/adding_context_menus_in_your_app

  1. For Tableview you can implement like below. when long press it will be invoked

    - (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point  API_AVAILABLE(ios(13.0)) {
     UIContextMenuConfiguration *config = [UIContextMenuConfiguration configurationWithIdentifier:@"MenuControls" previewProvider:nil  actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
    
         NSMutableArray *actions = [[NSMutableArray alloc] init];
         [actions addObject:[UIAction actionWithTitle:@"Copy" image:[UIImage systemImageNamed:@"doc.on.doc.fill"] identifier:nil handler:^(__kindof UIAction * _Nonnull action) {
             // Do corresponding action based on menu item
             [UIPasteboard generalPasteboard].string = @"Sample Data";
         }]];
    
         if (@available(iOS 14.0, *)) {
             UIMenu *menu = [UIMenu menuWithChildren:actions];
             return menu;
         } else {
             // Fallback on earlier versions
             UIMenu *menu = [UIMenu menuWithTitle:@"Actions" children:actions];
             return menu;
         }
     }];
    
     return config;
    }
    
  2. For collection view same can be achieved by using below method

    - (nullable UIContextMenuConfiguration *)collectionView:(UICollectionView *)collectionView contextMenuConfigurationForItemAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos, tvOS);
    

https://kylebashour.com/posts/ios-13-context-menus