Search code examples
iosobjective-cuitoolbaruialertcontroller

UIAlertController shown on BarButtonItem not centered properly


I am using the following code to show the UIAlertController on a toolBarButtonItem,

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }]];     

    [alertController addAction:[UIAlertAction actionWithTitle:@"Delete Selected Accounts" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [Utility showAlertWithTitle:nil message:DELETE_ACCOUNT delegate:self tag:1 buttons:@[@"Cancel", @"Delete"]];
    }]];
    alertController.popoverPresentationController.barButtonItem = self.moreButtonItem;
    [self presentViewController:alertController animated:YES completion:nil];

The alertController shown using this code appears right sided not centered. I want the alertController to appear exactly at center position over the toolBarButtonItem.


Solution

  • try adding below code

     alertController.popoverPresentationController.sourceView = senderView; //your view
     alertController.popoverPresentationController.sourceRect = senderView.bounds; //your view
     alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    

    This fixed my issue.