20

sto usando UIAlertController. Ma su iPad con iOS 8, actionSheet mostra con la freccia popover. Qualche idea per nascondere quella freccia?Cambia azione popover freccia in iOS8

Ecco il mio codice:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet]; 

      UIAlertAction *cancelAction = [UIAlertAction 
              actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
              style:UIAlertActionStyleCancel 
              handler:^(UIAlertAction *action) 
              { 
               NSLog(@"Cancel action"); 
              }]; 

      UIAlertAction *okAction = [UIAlertAction 
             actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
             style:UIAlertActionStyleDefault 
             handler:^(UIAlertAction *action) 
             { 
              NSLog(@"OK action"); 
             }]; 

      UIAlertAction *deleteAction = [UIAlertAction 
              actionWithTitle:NSLocalizedString(@"Delete", @"Delete action") 
              style:UIAlertActionStyleDestructive 
              handler:^(UIAlertAction *action) { 
               NSLog(@"Delete action"); 
              }]; 

      [alertController addAction:cancelAction]; 
      [alertController addAction:okAction]; 
      [alertController addAction:deleteAction]; 

      UIPopoverPresentationController *popover = alertController.popoverPresentationController; 
      if (popover) { 
       popover.sourceView = self.view; 
       popover.sourceRect = self.view.bounds; 
       popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown; 
      } 
      [self presentViewController:alertController animated:YES completion:nil]; 
+0

http://stackoverflow.com/questions/ 4755786/how-to-remove-ipads-popover-arrow-and-its-frame-border – Alfa

+0

utilizza [alertControllerActionSheet.popoverPresentationController setPermittedArrowDirections: 0]; – Jageen

risposta

37

Soluzione:
use below line for remove arrow from action sheet

[yourAlertController.popoverPresentationController setPermittedArrowDirections:0]; 


Esempio

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Test Action Sheet" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction *cancelAction = [UIAlertAction 
            actionWithTitle:@"Cancel" 
            style:UIAlertActionStyleDestructive 
            handler:^(UIAlertAction *action) 
            { 
             NSLog(@"Cancel action"); 
            }]; 

    UIAlertAction *okAction = [UIAlertAction 
           actionWithTitle:@"Ok" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction *action) 
           { 
            NSLog(@"OK action"); 
           }]; 
    UIAlertAction *otherAction = [UIAlertAction 
           actionWithTitle:@"Other" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction *action) 
           { 
            NSLog(@"Otheraction"); 
           }]; 

    [alertController addAction:okAction]; 
    [alertController addAction:otherAction]; 
    [alertController addAction:cancelAction]; 


    // Remove arrow from action sheet. 
    [alertController.popoverPresentationController setPermittedArrowDirections:0]; 

    //For set action sheet to middle of view. 
    alertController.popoverPresentationController.sourceView = self.view; 
    alertController.popoverPresentationController.sourceRect = self.view.bounds; 

    [self presentViewController:alertController animated:YES completion:nil]; 

uscita

enter image description here

+2

rect.orign.x = ... e .y == ... non sono richiesti. – tuoxie007

-1

Sei sulla strada sbagliata con UIPopoverPresentationController per visualizzare un avviso. È solo che non c'è bisogno che stato tagliato di codice ...

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet]; 

     UIAlertAction *cancelAction = [UIAlertAction 
             actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
             style:UIAlertActionStyleCancel 
             handler:^(UIAlertAction *action) 
             { 
              NSLog(@"Cancel action"); 
             }]; 

     UIAlertAction *okAction = [UIAlertAction 
            actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction *action) 
            { 
             NSLog(@"OK action"); 
            }]; 

     UIAlertAction *deleteAction = [UIAlertAction 
             actionWithTitle:NSLocalizedString(@"Delete", @"Delete action") 
             style:UIAlertActionStyleDestructive 
             handler:^(UIAlertAction *action) { 
              NSLog(@"Delete action"); 
             }]; 

     [alertController addAction:cancelAction]; 
     [alertController addAction:okAction]; 
     [alertController addAction:deleteAction]; 

     [self presentViewController:alertController animated:YES completion:nil]; 
+2

Non funzionerà su ipad. – TienLe

6

risposta di Jageen, a Swift:

popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0) 
6

La risposta selezionata non centrare l'avviso se si dispone di una barra di navigazione/stato. Per centrare esattamente il controller avviso:

alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController]; 
alertController.popoverPresentationController.sourceView = self.view; 
alertController.popoverPresentationController.permittedArrowDirections = 0; 

Con il metodo comodo:

- (CGRect)sourceRectForCenteredAlertController 
{ 
    CGRect sourceRect = CGRectZero; 
    sourceRect.origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.origin.x/2.0; 
    sourceRect.origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.origin.y/2.0; 
    return sourceRect; 
} 

Inoltre, il controller di allarme non rimane centrato se la vista viene ruotata. Per mantenere centrato il controller di allerta è necessario aggiornare il sourceRect dopo la rotazione. Per esempio:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    // Check if your alert controller is still being presented 
    if (alertController.presentingViewController) { 
     alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController]; 
    } 
} 

Oppure, se non si desidera utilizzare gli eventi di rotazione, è possibile utilizzare il metodo popoverPresentationController delegato per riposizionare il popover:

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view 
{ 
    // Re-center with new rect 
}