简体   繁体   中英

UIPopoverController is not presenting controller in assigned position

I am trying to present a popover on clicking a button. When I tap on button, it shows popover on top of the screen and background is completely black. I have set the position and gave hard coded origin to popover but still it is going on top.

Here is the snapshot:

在此处输入图片说明

Here is the code I am using:

  UIPopoverController *popOverController = [[UIPopoverController  alloc]
                                            initWithContentViewController:myViewController];
  CGRect frame = CGRectMake(20, 30, 100, 50);
  popOverController.popoverLayoutMargins = UIEdgeInsetsMake(0, frame.origin.x, 0, 0);
  popOverController.delegate = self;
  [popOverController setBackgroundColor:[UIColor clearColor]];
  [popOverController presentPopoverFromRect:frame
                                inView:self.view
              permittedArrowDirections:UIPopoverArrowDirectionAny
                              animated:YES];

EDIT I have changed my code and tried this:

  UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, 50)];
  callOut.backgroundColor = [UIColor whiteColor];

  UILabel *callOutLable = [[UILabel alloc] initWithFrame:CGRectMake(callOut.frame.origin.x-5, callOut.frame.origin.y-5, 80, 40)];
  [callOutLable setText:@"Callout"];
  [callOut addSubview:callOutLable];


  UIViewController *controller = [[UIViewController alloc] init];
  controller.view = callOut;

  UIPopoverController *popOverController = [[UIPopoverController  alloc]
                                            initWithContentViewController:controller];
  popOverController.delegate = self;
  CGRect frame = callOut.frame;
  frame.size.height = 100;
  frame.size.width = 100;
  popOverController.popoverContentSize = CGSizeMake(100.0, 50.0);
  [popOverController setBackgroundColor:[UIColor blueColor]];
  [popOverController presentPopoverFromRect:frame
                                inView:self.view
              permittedArrowDirections:UIPopoverArrowDirectionAny
                              animated:YES];

As I am creating a custom view which is really small. Now it shows the label on assigned position but still the background view is white.

Now what I am missing here? Any clue?

 TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
testViewController.delegate = self;

self.userDataPopover = [[UIPopoverController alloc] initWithContentViewController:testViewController];
self.userDataPopover.popoverContentSize = CGSizeMake(320.0, 400.0);
[self.userDataPopover presentPopoverFromRect:[(UIButton *)sender frame]
                                   inView:self.view
                 permittedArrowDirections:UIPopoverArrowDirectionAny
                                 animated:YES];

See this example, you should give content size to pop over as above. But you declared directly as CGRect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM