How to implement UIAlertcontroller as PopOver(with arrow direction up) in a UIBarbutton

一笑奈何 提交于 2019-12-11 14:24:22

问题


I know that this is old school question - but I did searched the web and found solutions to be deprecated. How would I implement a UIAlertcontroller as popOver(with arrow direction up) in a barButton. Here's the code:

 - (IBAction)eventSortingAction:(UIBarButtonItem *)sender {

UIAlertController * view=   [UIAlertController
                         alertControllerWithTitle:@"My Title"
                         message:@"Select you Choice"
                         preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* ok = [UIAlertAction
                 actionWithTitle:@"OK"
                 style:UIAlertActionStyleDefault
                 handler:^(UIAlertAction * action) {
                     //Do some thing here
                     [view dismissViewControllerAnimated:YES completion:nil];

                 }];
UIAlertAction* cancel = [UIAlertAction
                     actionWithTitle:@"Cancel"
                     style:UIAlertActionStyleCancel
                     handler:^(UIAlertAction * action) {
                         [view dismissViewControllerAnimated:YES completion:nil];

                     }];
[view addAction:ok];
[view addAction:cancel];
[view setModalPresentationStyle:UIModalPresentationPopover];
view.modalInPopover = YES;
view.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
view.popoverPresentationController.delegate = self;
[self presentViewController:view animated:YES completion:nil];

UIView* senderView = [sender valueForKey:@"view"]; //HACK
UIPopoverPresentationController* popover = view.popoverPresentationController;
if (popover) {
   popover.sourceView = senderView;
   popover.sourceRect = senderView.bounds;
   popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
   popover.barButtonItem = self.actionBarButton;
   popover.delegate = self;
}}

apparently I always got a "popover = nil". Please Help! Thanks in advance!

By the way this code is not mine, just testing it in Xcode.


回答1:


Just a reminded because this is a top result on Google :

popPresenter.barButtonItem is an alternative to popPresenter.sourceView+popPresenter.sourceRect

See (source)

Regarding OP question, IBAction parameter sender should be used.

UIPopoverPresentationController *popPresenter = [alertController
                                             popoverPresentationController];
popPresenter.barButtonItem = sender;
[self presentViewController:alertController animated:YES completion:nil];



回答2:


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

UIAlertAction *actnCamera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                   }];

UIAlertAction *actnLibrary = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * action) {
                                                    }];

[alertController addAction:actnLibrary];
[alertController addAction:actnCamera];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertController
                                                 popoverPresentationController];
popPresenter.sourceView = self.view;
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.x = self.navigationItem.leftBarButtonItem.width;
popPresenter.sourceRect = frame;
popPresenter.barButtonItem = self.navigationItem.leftBarButtonItem;
[self presentViewController:alertController animated:YES completion:nil];

OUTPUT



来源:https://stackoverflow.com/questions/36830807/how-to-implement-uialertcontroller-as-popoverwith-arrow-direction-up-in-a-uiba

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