简体   繁体   English

在iOS8中更改操作表弹出箭头

[英]Change Action sheet popover arrow in iOS8

i'm using UIAlertController . 我正在使用UIAlertController But on iPad with iOS 8, actionSheet show with popover arrow. 但是在iOS 8的iPad上,actionSheet显示了弹出式箭头。 Any ideas to hide that arrow? 任何隐藏箭头的想法?

Here is my 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];

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

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

[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];


Sample 样品

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

Output 产量

在此输入图像描述

Jageen在Swift中的回答:

popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

The selected answer does not center the alert if you have a nav/status bar. 如果您有导航/状态栏,则所选答案不会使警报居中。 To exactly center your alert controller: 要使警报控制器准确居中:

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

With the convenience method: 方便的方法:

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

Also, the alert controller does not stay centered if the view is rotated. 此外,如果旋转视图,警报控制器不会保持居中。 To keep the alert controller centered you need to update the sourceRect after rotation. 要使警报控制器保持居中,您需要在旋转后更新sourceRect。 For example: 例如:

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

Or, if you do not want to use rotation events, you can use the popoverPresentationController delegate method to reposition the popover: 或者,如果您不想使用旋转事件,可以使用popoverPresentationController委托方法重新定位弹出窗口:

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

You're on the wrong track using UIPopoverPresentationController to display an alert. 您使用UIPopoverPresentationController在错误的轨道上显示警报。 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM