【问题标题】:Change Action sheet popover arrow in iOS8在 iOS 8 中更改 Actionsheet 弹出箭头
【发布时间】:2014-11-10 23:48:06
【问题描述】:

我正在使用 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];

【问题讨论】:

标签: ipad ios8 uipopovercontroller uiactionsheet uialertcontroller


【解决方案1】:

解决方案:
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];

输出

【讨论】:

  • rect.orign.x = ... 和 .y == ... 不是必需的。
【解决方案2】:

Jageen 在 Swift 中的回答:

popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

【讨论】:

    【解决方案3】:

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

    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
    }
    

    【讨论】:

      【解决方案4】:

      使用 UIPopoverPresentationController 显示警报时您走错了路。你只是不需要那段代码......

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-05
        • 2019-01-01
        • 2015-11-22
        • 1970-01-01
        • 2019-08-27
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多