Change Action sheet popover arrow in iOS8

試著忘記壹切 提交于 2019-12-20 17:26:33

问题


i'm using UIAlertController . But on iPad with iOS 8, actionSheet show with popover arrow. 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];

回答1:


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




回答2:


Jageen's answer, in Swift:

popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)



回答3:


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. 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:

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



回答4:


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


来源:https://stackoverflow.com/questions/25882133/change-action-sheet-popover-arrow-in-ios8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!