Wrong position of popover in scrollview iOS - ios

I have a button that will show the popover. The popover must appear under the button. But it appears somewhere on the screen.
Here's My Code for the popover
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320,340)]; 
[popoverView addSubview:tblViewMenu];
popoverContent.view = popoverView;
popoverContent .contentSizeForViewInPopover = CGSizeMake(620,620);
popPickerController = [[UIPopoverController alloc]initWithContentViewController:popoverContent];
[popPickerController presentPopoverFromRect:anchor.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUpanimated:YES];
BTW this popover will show inside a scrollview.

Use following code for present popoverView.
[popover presentPopoverFromRect:button.frame inView:self.scrollView permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];

Give immediate parent of button as parameter value inView:
[popPickerController presentPopoverFromRect:anchor.frame
inView:anchor.superview
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
Always make sure to give right parent while presenting popover.

[ur popovername presentPopoverFromRect:[(UIButton *)sender frame] inView:anchor.superview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Related

How to display a UIPopover from another class?

I have a iPad app (XCode 5, iOS 6, ARC and Storyboards), which has a class to display a UIView. From a separate class, I want to display a UIPopover under a certain condition.
I'm having a problem with the last line of code:
// create a popover for login or registration
UIViewController* popoverContent = [[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 180)];
popoverView.backgroundColor = [UIColor lightGrayColor];
popoverContent.view = popoverView;
//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 280); // was 180
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
// if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
// [popoverController presentPopoverFromRect:popoverView inView:self
// permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
SettingsViewController *svc = [[SettingsViewController alloc]init];
[popoverController presentPopoverFromRect: popoverView inView:svc.view.frame
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
It's telling me:
Sending 'UIView *__strong' to parameter of incompatible type 'CGRect' (aka 'struct CGRect')
How do I fix this? (I've tried different ways of presenting the popover, but none of them work).
Assuming the popoverView is the section of the svc view you want the popover to present from, your code should read:
[popoverController presentPopoverFromRect:popoverView.frame inView:svc.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
This will correct that error.
Edit: The reason it's probably not displaying for you is because you seem to be misunderstanding the presentPopoverFromRect method. The frame you indicate in the presentPopoverFromRect method needs to correspond the point of the view you'd like the popover to emerge from. For example, to have the popover present from the top left corner of the view, do something like this:
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:svc.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Can't change size for UIPopover?

I'm writing an app that needs to spawn a popover in order to add a new note. To that end, I have something that does kind of the trick, however, I can't seem to adjust the size of the popover. This is how I'm spawning it:
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
CGRect rectFoo = CGRectMake(0, 0, 100, 100);
UIView* fooView = [[UIView alloc] initWithFrame:rectFoo];
[fooView setBackgroundColor:[UIColor redColor]];
[fooTroller setView:fooView];
popOver =[[UIPopoverController alloc] initWithContentViewController:fooTroller];
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
Any thoughts? It's not respecting the view size.
You should not be calling setView: on the view controller. Let the view controller setup its own view.
The proper way to size a popover is to either override the contentSizeForViewInPopover method of your view controller to return the size or to set the popoverContentSize property on the popover.
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
popOver = [[UIPopoverController alloc] initWithContentViewController:fooTroller];
popOver.popoverContentSize = CGSizeMake(100, 100);
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];

How to attach a UIPopover to a rectangle I have drawn

I have an iPad app (XCode 4.6, ios 6.2, ARC and Storyboards). I have drawn a GCRect on a position of a UIView.
CGRect rectangle = CGRectMake( [appSelected.aPosX floatValue], [appSelected.aPosY floatValue],[appSelected.aPosW floatValue], [appSelected.aPosH floatValue]);
I have a method that does the drawing for me; this is the code for that method:
-(void)showHTMLHelp:(NSString *)htmlString pointTo:(id)target background:(UIColor *)bgColor {
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0]; // frame color?
popoverContent.view = popoverView;
//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
// add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame:popoverView.frame];
webView.backgroundColor = [UIColor whiteColor]; // change background color here
// add the webView to the popover
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:nil]];
[popoverView addSubview:webView];
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//present the popover view non-modal with a refrence to the button pressed within the current view
if([target isKindOfClass: [UITextField class]])
[popoverController presentPopoverFromRect:((UITextField *)target).frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else if([target isKindOfClass: [UISegmentedControl class]])
[popoverController presentPopoverFromRect:((UISegmentedControl *)target).frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else if([target isKindOfClass: [UIButton class]])
[popoverController presentPopoverFromRect:((UIButton *)target).frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else
[popoverController presentPopoverFromRect:*(CGRect *)CFBridgingRetain(target)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Now I want to have a UIPopover point to that rectangle. Since GCRect is a C structure, I can't figure out how to do it. This is what I tried, but obviously it's wrong. How can I do this? This is the code where I call the method to display the popover:
PreferencesViewController *pvc = [[PreferencesViewController alloc] init];
[pvc showHTMLHelp:html pointTo: rectangle background:[UIColor whiteColor]];
There is absolutely no problem with passing a C structure (or C primitive variable) to an Objective-C method. In fact, presentPopoverFromRect:inView:permittedArrowDirections:animated: takes a simple CGRect as you can tell by the header (or the documentation):
- (void)presentPopoverFromRect:(CGRect)rect
inView:(UIView *)view
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
Simply call it like this:
[popoverController presentPopoverFromRect:rectangle
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
And as Mar0ux already pointed out, it's CGRect, not GCRect. CG stands for Core Graphics.
May i ask you why you are sending an CGRect and casting it to 'id'? Where is the sense?! I would prefer to change your parameter type to CGRect too.
Kind regards.

Keep popovercontentsize constant for all views in navigationcontroller

When I pass my UINavigationController to UIPopOver and set its popOverContentSize property, it remains in effect for only first view. When I navigate to next view, the popOverContent size again expands to full view which is unwanted. How can I restrict the constant size of popOver for all views in navigation controller?
- (void)selectTextStyle:(id)sender {
DJTextStyleMasterViewController *textStyle = [[DJTextStyleMasterViewController alloc] init];
self.navController = [[UINavigationController alloc] initWithRootViewController:textStyle];
UIPopoverController *popOver = [[UIPopoverController alloc]
initWithContentViewController:self.navController];
[popOver setDelegate:self];
[popOver setPopoverContentSize:CGSizeMake(320, 400)];
[popOver presentPopoverFromBarButtonItem:self.textStyleButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[textStyle release];
}
within the first view controller in the popover controller you could use this method:
[self setContentSizeForViewInPopover:CGSizeMake(320, 400)];
Then the view of the popover (if it isn't manually modified) should be the same also for the next view controller.

UIPopover Issue

Trying to display my popover from the tableView cell that has been selected
UITableViewCell *cell;
UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController];
[pop presentPopoverFromRect:cell.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[switchV release];
What am I doing wrong?
You have a button in a table view cell and you want to display a popover pointing to that cell when the button is pressed.
First, add the button to the cell in cellForRowAtIndexPath using something like this:
(doesn't have to be rounded rect)
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 0, 100, 30)];
[button setTitle:#"Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(profileUser:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
An important point above is that in the #selector, there is a colon after profileUser (this tells the button to send a reference to itself as the first parameter to profileUser). This reference can be used to figure out which cell was selected.
The profileUser: method should be something like this:
-(void)profileUser:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
//first superview is cell.contentView
//second superview is cell
UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController];
[pop presentPopoverFromRect:cell.frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[switchV release];
[navController release];
self.popoverController = pop; //save in property for later release
[pop release];
}
If possible, leave the arrow direction as UIPopoverArrowDirectionAny and let it figure out the best place to put it.
Edit:
To show the popover with the arrow pointing up to the button instead of the cell, use this:
[pop presentPopoverFromRect:button.frame inView:cell
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
However, depending on the position of the cell on the screen, the popover may not look right underneath the button. Use "Any" instead of "Up" unless you're sure of the results.
Also note you should save a reference to the popover controller for later release (in dealloc) otherwise the [pop release] in the method may cause a crash. For a detailed example, see the sample app Popovers.

Resources