Swift: Getting 'Snapshotting a view that has not been rendered..' error when trying to open a URL in safari from my app

前端 未结 8 1619
一整个雨季
一整个雨季 2020-12-28 12:48

One of the specifications of my app is that on tapping a tableView cell, the user will be redirected to the website associated with the cell. Here is the code:



        
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 13:32

    I'm triggering a UIAlertControllerStyleActionSheet UIAlertController after the user clicks a UITableViewRowAction, and I got the same error message repeated 8 times after presenting the UIAlertController.

    On the iPad Pro Simulator for iOS 9.3, I tried Saliom's answer, and I went from eight log messages to one.

    As anorskdev suggested, I put my call to -[UIView layoutIfNeeded] after my -[UIViewController presentViewController:animated:completion:] call, and all the warnings disappeared:

    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UITableViewRowAction *moreAction =
      [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                         title:@"More"
                                       handler:^(UITableViewRowAction * _Nonnull action,
                                                 NSIndexPath * _Nonnull moreIndexPath)
      {
        UIAlertController *alertController = 
        [UIAlertController alertControllerWithTitle:name 
                                            message:nil
                                     preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *deleteAlertAction = 
        [UIAlertAction actionWithTitle:@"Delete"
                                 style:UIAlertActionStyleDestructive
                               handler:deleteAction];
        [alertController addAction:deleteAlertAction];
    
        UIAlertAction *cancelAlertAction = 
        [UIAlertAction actionWithTitle:@"Cancel"
                                 // totally ok to use the proper
                                 // UIAlertActionStyleCancel
                                 style:UIAlertActionStyleCancel
                               handler:cancelAction];
        [alertController addAction:cancelAlertAction];
    
        CGRect sourceRect = [self.tableView rectForRowAtIndexPath:moreIndexPath];
    
        // You must specify a sourceRect and sourceView
        // or a barButtonItem or presenting a
        // UIAlertControllerStyleActionSheet UIAlertController
        // will crash on iPad.
        alertController.popoverPresentationController.sourceRect = sourceRect;
        alertController.popoverPresentationController.sourceView = self.tableView;
        // first, present the alertController
        [self presentViewController:alertController
                           animated:YES
                         completion:nil];
        // then -layoutIfNeeded
        [alertController.view layoutIfNeeded];
      }
      return @[
              moreAction,
              ];
    }
    

    Notice that it wasn't necessary to use Patrick's solution of using UIAlertActionStyleDefault for cancelAlertAction or nurider's solution of eliminating it altogether on iPads.

提交回复
热议问题