22

我正在使用UIAlertController。但是在装有 iOS 8 的 iPad 上,actionSheet 显示带有弹出箭头。有什么想法可以隐藏那个箭头吗?

这是我的代码:

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];
4

4 回答 4

42

解决方案 :
use below line for remove arrow from action sheet

[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];


样本

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];

输出

在此处输入图像描述

于 2014-09-17T04:58:15.133 回答
7

Jageen 在 Swift 中的回答:

popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
于 2015-08-29T16:00:56.950 回答
6

如果您有导航/状态栏,则所选答案不会使警报居中。要使警报控制器准确居中:

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

使用方便的方法:

- (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;
}

此外,如果视图旋转,警报控制器不会保持居中。要使警报控制器居中,您需要在旋转后更新 sourceRect 。例如:

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

或者,如果您不想使用旋转事件,可以使用popoverPresentationController委托方法重新定位弹出框:

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing  _Nonnull *)view
{ 
    // Re-center with new rect
}
于 2015-09-23T22:50:08.710 回答
-1

You're on the wrong track using UIPopoverPresentationController to display an alert. You just do not need that snipped of code...

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];
于 2014-09-17T04:32:54.693 回答