Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UIPopOverController size

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.

screen shot of the error

like image 440
Andrew Avatar asked Jul 21 '11 03:07

Andrew


2 Answers

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];
like image 119
RedBlueThing Avatar answered Sep 16 '22 14:09

RedBlueThing


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?

like image 43
ott-- Avatar answered Sep 19 '22 14:09

ott--