Could anyone please tell me what the rect and view should be? I do not understand what I shall pass to the selector. An example would be great!
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
I use this pretty often. Let's say you want to tap on an image and present a popover with information about it. Assuming you have a gesture recognizer with the selector method (handleImageTap:) on your image, here would be an example code to make that happen:
- (void)handleImageTap:(UIGestureRecognizer *)gesture {
// initialize your popover view controller and assign it to your popoverController
MyPopoverViewController *content = [[MyPopoverViewController alloc] init];
popoverController = [[UIPopoverController alloc] initWithContentViewController:content];
popoverController.popoverContentSize = CGSizeMake(600, 600);
popoverController.delegate = self;
[content release];
if (popoverController.popoverVisible == NO) {
// you can find the tappedImage through the gesture by searching up superviews if you don't already have a reference to it;
[popoverController presentPopoverFromRect:[tappedImage frame] inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[popoverController dismissPopoverAnimated:YES];
}
}
So basically, view will be self.view becuase you are displaying it from the current view controller. The rect is just whatever rect you want the popover to display from. In this case, it is set up to be displayed from the frame of an image. I hope this helps you. If something is still confusing, I'll be happy to try and clear it up
Related
How can you dismiss a popover from the contentViewController, so in the example code I have below I would like to dismiss the UIPopOver from the ProfileViewController code. How do I do this? Another post similar to this suggested to use NSNotification, but how do use it?
- (void)profilePop:(UITapGestureRecognizer *)recognizer
{
ProfileViewController * profile = [[ProfileViewController alloc] init];
CGPoint location = [recognizer locationInView:self.table];
NSIndexPath* indexPath = [self.table indexPathForRowAtPoint:location];
ConvoreCell* cell = (ConvoreCell *) [self.table cellForRowAtIndexPath:indexPath];
profile.uid = [[[self.posts objectAtIndex:indexPath.row] creator] mid];
UIPopoverController * profilePop = [[UIPopoverController alloc] initWithContentViewController:profile];
[profilePop setPopoverContentSize:CGSizeMake(350, 180)];
[profilePop presentPopoverFromRect:CGRectMake(0,0, cell.avatar.frame.size.width, cell.avatar.frame.size.height) inView:cell permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
I would keep a reference to the popover in your profile class.
profile.popOver = profilePop;
then in the area you'de like to dismiss:
[self.popover dismissPopoverAnimated:YES];
I think keeping a reference is the way to go too, but I don't know if its a good idea to retain it, because when you need to release the popover controller it won't dealloc because there is an extra retain.
I have a view with a bunch of button in a UIScrollView. When the user presses a button, I want a UIPopOverController to display pointing at the selected button. It kind of works, but the popover is the wrong size and points to a random point in the view. Here is my code.
-(void)detail:(id)sender{
UIButton *button = sender;
NSLog(#"tag = %i", button.tag);
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:navController];
self.popover.delegate = self;
[self.popover presentPopoverFromRect:button.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Than the problem with the size of the popover:
In the view that is inside the popover, I have:
self.contentSizeForViewInPopover = scroll.contentSize;
NSLog(#"%f, %f", scroll.contentSize.height, scroll.contentSize.width);
NSLog(#"showing: %f, %f", self.contentSizeForViewInPopover.height, self.contentSizeForViewInPopover.width);
and both logs are matching. So I think everything should work correctly. But it doesn't. Here is a screen shot. Let me know if you need more of my code. Thanks in advance.
First thing I noticed is that you should be using the button.frame not the button.bounds for the from rectangle. The difference between these UIView properties:
The geometry of a view is defined by its frame, bounds, and center
properties. The frame property defines the origin and dimensions of
the view in the coordinate system of its superview and is commonly
used during layout to adjust the size or position of the view.
The bounds property defines the internal dimensions of the view as it
sees them and is used almost exclusively in custom drawing code.
You can set the height of your popover controller using its setPopoverContentSize message:
// so something like this ...
[self.popover setPopoverContentSize:CGSizeMake(320, 460)];
// and to bind the popover to your button
[self.popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Regarding the size of the popover only: I managed to do it with a variable heighted UILabel:
UILabel *hinweis;
hinweis.text = #"...";
hinweis.frame = CGRectMake(x,y,width,800);
[hinweis sizeToFit];
And for the arrow: have you tried a different inView param like self.parentViewController.view?
Is it possible to move a UIPopovercontroller around the screen once its already been presented?
I have a small view in my app that can move slightly, and I would like a UIPopoverController to move around with it without having to re-present the UIPopoverController every time it moves. Is this possible?
As of iOS 9, "UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIModalPresentationPopover and UIPopoverPresentationController."
Use the following to present a popover. You may be able to change the values of sourceRect and sourceView but this isn't intended API behavior.
DetailViewController *detailVC = self.detailViewController;
detailVC.preferredContentSize = CGSizeMake(420, 92);
detailVC.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *presController = detailVC.popoverPresentationController;
if (presController) {
presController.delegate = detailVC;
presController.barButtonItem = self.detailButton;
presController.sourceRect = self.view.frame;
presController.sourceView = self.view;
presController.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController: detailVC animated:YES completion:^{
}];
Instead, I suggest keeping a reference to the view controller presented with the popover, dismiss the popover and re-present.
DetailViewController *detailVC = self.detailViewController;
[self dismissViewControllerAnimated:NO completion:^{
UIPopoverPresentationController *presController = detailVC.popoverPresentationController;
if (presController) {
presController.delegate = detailVC;
presController.barButtonItem = self.detailButton;
presController.sourceRect = self.view.frame; //Update frame
presController.sourceView = self.view;
presController.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController: detailVC animated:NO completion:^{
}];
}];
You can re-call presentPopoverFromRect.
See http://developer.apple.com/library/ios/#qa/qa1694/_index.html
No you can't. This is no method to do that.
pover presentPopoverFromRect:
was mentioned in the comments, but notice the
present
you cannot represent without redrawing.
You can track the users touches around the screen, and use presentPopoverFromRect: to reshow the popover.
I think the issue here is that I'm trying to call a mediaPicker and that doesn't support other orientations...
Does anyone have a fix for this?
Here is my current code:
- (IBAction)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES; // this is the default
mediaPicker.modalPresentationStyle = UIModalPresentationPageSheet;
//mediaPicker.prompt = #"Select items to play";
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
// Init a Navigation Controller, using the MediaPicker as its root view controller
UINavigationController *theNavController = [[UINavigationController alloc] initWithRootViewController:mediaPicker];
[theNavController setNavigationBarHidden:YES];
// Init the Popover Controller, using the navigation controller as its root view controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:theNavController];
// Make a rect at the size and location of the button I use to invoke the popover
CGRect popOverRect = chooseMusicButton.frame;
// Specify the size of the popover
CGSize MySize = CGSizeMake(520.0, 720.0);
[popoverController setPopoverContentSize:MySize animated:YES];
// Display the popover
[popoverController presentPopoverFromRect:popOverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.delegate = self;
}
This code is overly complicated. First you present the media picker modally, then you present it as a popover; why? In the popover, you stuff it into a navigation controller before presenting it; why? Presenting a media picker on iPad is much simpler than that:
MPMediaPickerController* picker =
[[[MPMediaPickerController alloc] init] autorelease];
picker.delegate = self;
UIPopoverController* pop =
[[UIPopoverController alloc] initWithContentViewController:picker];
self.currentPop = pop;
[pop presentPopoverFromRect:[sender bounds] inView:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[pop release];
That works in any orientation and even survives rotation while the popover is showing.
All pre-defined modal controllers support all orientations but they must be presented from the root view controller for them to behave correctly in orientation and rotation. My guess is that that the "self" in your code is not the root view controller. You may have to re-architect the code a bit to make this happen if possible.
There are other hacks I have seen to make it work without being presented from the root view controller but they all seemed to be asking for trouble such as extending UIViewController with a category to over-ride interfaceOrientation.
If you can present it from the root view controller, it would be the simplest and cleanest but I realize it is not always possible (e.g., it is inside a library you are providing to third party apps to embed).
I have an "add Contact" button which when on iPhone I present a navigation controller with root view controller of an ABNewPersonController modally.
If on iPad I have got a popover which I can display with the new person controller inside - nice.
The problem comes when trying to dismiss.
I can dismiss the popover when touching done or cancel within my implementation of didCompleteWithNewPerson using;
if(self.popoverController != nil)
[popoverController dismissPopoverAnimated:YES];
However, this doesn't dismiss when touching outside the popover.
I've returned YES for my popoverControllerShouldDismissPopover method and set the delegate of my popover to this. I've put an NSLOG inside this method and it's not dropping in there - Am I missing something?
Does anyone know how to dismiss the popover when touching outside?
Update - More Code
-(IBAction)contactsClicked:(id) sender{
ABNewPersonViewController *newPersonView = [[ABNewPersonViewController alloc] init];
[newPersonView setNewPersonViewDelegate:self];
[newPersonView setDisplayedPerson:newPerson];
UINavigationController *addContactNavController = [[UINavigationController alloc] initWithRootViewController:newPersonView];
[newPersonView release];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if(self.popoverController == nil){
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
self.popoverController = popover;
self.popoverController.delegate = self;
[popover release];
}
CGRect frame = [sender frame];
[popoverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
} else {
[self presentModalViewController:addContactNavController animated:YES];
[addContactNavController release];
}
}
-(void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person{
[self dismissModalViewControllerAnimated:YES];
}
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
NSLog(#"DONE OR CANCEL clicked!!!!"); //prints
if (self.popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
[self dismissModalViewControllerAnimated:YES];
}
The Done and Cancel buttons of the new person controller work, dismissing the controller and the popover (when running on iPad). I guess this means the delegate for the ABNewPersonViewController is implemented correctly. (?)
I'm guessing that I may be confusing the issue by having multiple controllers and my popover delegate method is getting hidden or something?
Thanks in advance
EDIT - Delegate method
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)thePopoverController{
NSLog(#"clicked outside the popover");//never prints
return YES;
}
From the docs:
Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.
You should use the popover delegate methods –popoverControllerShouldDismissPopover: to listen for when it's about to be dismissed and do your saving etc. there.
Also, you should use self not this.