ipad 中的 UIPopoverController的使用

来源:互联网 发布:固态硬盘安装教程 知乎 编辑:程序博客网 时间:2024/06/08 15:17

1、初始化UIPopoverController

    UIPopoverController *popover = [[UIPopoverController allocinitWithContentViewController:navigationController];   

//必须是个 ViewController

/*且该 ViewController的 viewDidLoad方法中要设置其在popover中的尺寸,如

    self.contentSizeForViewInPopover = CGSizeMake(300.0280.0);

*/


    self.recentSearchesPopoverController = popover;

    recentSearchesPopoverController.delegate = self;    //self要实现UIPopoverControllerDelegate协议

 

2、设置当popover弹出时,用户仍可以交互的视图

    // Ensure the popover is not dismissed if the user taps in the search bar.

    popover.passthroughViews = [NSArray arrayWithObject:searchBar];

 

 

3、展现popover

    [self.recentSearchesPopoverController presentPopoverFromRect:self.searchBar.bounds

                                                          inView:self.searchBar

                                        permittedArrowDirections:UIPopoverArrowDirectionAny

                                                        animated:NO];


 

4、消失popover

//dismiss the popover.

    [recentSearchesPopoverController dismissPopoverAnimated:YES];


 

5、实现 UIPopoverControllerDelegate协议的方法

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {

 


}

重要补充:设置弹出窗的位置

popover.popoverContentSize = CGSizeMake(300, 300); //弹出窗口大小,如果屏幕画不下,会挤小的。这个值默认是320x1100

CGRect popoverRect = CGRectMake(200, 700, 10, 10);

[popover presentPopoverFromRect:popoverRect  //popoverRect的中心点是用来画箭头的,如果中心点如果出了屏幕,系统会优化到窗口边缘

                                          inView:self.view //上面的矩形坐标是以这个view为参考的

              permittedArrowDirections:UIPopoverArrowDirectionDown  //箭头方向

                                      animated:YES];