UIPopoverController未在指定位置显示控制器

时间:2014-10-15 05:24:50

标签: ios uipopovercontroller

我试图在点击按钮时显示一个弹出窗口。当我点击按钮时,它会在屏幕顶部显示弹出窗口,背景完全是黑色。我已经设置了位置并给了弹出的硬编码来源,但它仍然是最重要的。

以下是快照:

enter image description here

以下是我正在使用的代码:

  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];

修改 我已经更改了我的代码并尝试了这个:

  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];

因为我正在创建一个非常小的自定义视图。现在它在指定位置显示标签,但背景视图仍为白色。

现在我在这里缺少什么?任何线索?

1 个答案:

答案 0 :(得分:0)

 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];

请参阅此示例,您应该像上面一样提供内容大小。但你直接宣布为CGRect。