问题
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