Change Action sheet popover arrow in iOS8

后端 未结 4 1221
挽巷
挽巷 2021-02-07 01:13

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:

         


        
4条回答
  •  粉色の甜心
    2021-02-07 01:57

    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
    }
    

提交回复
热议问题