弹出窗控制器:UIPopoverController

截图:

 
实质:就是将内容控制器包装成popoverController的形式,然后在模态出来,必须给定指向目标(target、frame)。
 
 
功能:它是ipd特有的特性,不适用iphone,用来点击一个按钮时,弹出一个窗口以显示一些信息。
如果没有使用passthroughViews属性设置过滤控件,那么点击屏幕的任意区域都可以关闭弹出窗;可是,如果设置了passthroughViews属性过滤了按钮的父视图和显示区域控件,那么点击屏幕任何区域都是无效的,只有点击按钮才会与用户发生交互。
 
介绍:它是直接继承自NSObject的控件,并不是UIViewController的子类,所以,它不具备可视化的条件。可是,既然它是弹出窗,就必须具有可视效果,如何实现呢?其实,它是通过创建第三方控制器来实现的,即内容控制器,该控制器继承于UIViewController,将其设置为UIPopoverController的内容控制器即可。
 

设置内容的尺寸有2种方法:
方法一:
@property (nonatomic) CGSize popoverContentSize;

- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;

以上方法和属性都是UIPopoverController的

方法二:
内容控制器可以自行设置自己在popover中显示的尺寸
p在iOS 7之前

@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;

从iOS 7开始

@property (nonatomic) CGSize preferredContentSize;

以上属性都是UIViewController的

 
类介绍:
 
箭头方向枚举:

typedef NS_OPTIONS(NSUInteger, UIPopoverArrowDirection) {

UIPopoverArrowDirectionUp = 1UL << 0,     //箭头往上指向按钮

UIPopoverArrowDirectionDown = 1UL << 1, //箭头往下指向按钮

UIPopoverArrowDirectionLeft = 1UL << 2,  //箭头往左指向按钮

UIPopoverArrowDirectionRight = 1UL << 3, //箭头往右指向按钮

UIPopoverArrowDirectionAny = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown | UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight,  //箭头方向自适应

UIPopoverArrowDirectionUnknown = NSUIntegerMax  //箭头方向未知

};

@interface UIPopoverController : NSObject <UIAppearanceContainer>

属性:

//弹出框控制器代理

@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;

//内容控制器

@property (nonatomic, retain) UIViewController *contentViewController;

//弹出窗是否可见的(只读属性)

@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;

//弹出窗的箭头方向

@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;

//过滤视图控件,即该数组中视图不可以与用户进行交互,但是其他区域的控件是可以的,一般选择按钮本身

@property (nonatomic, copy) NSArray *passthroughViews;

//弹出窗中内容区域大小

@property (nonatomic) CGSize popoverContentSize;

//弹出窗背景颜色

@property (nonatomic, copy) UIColor *backgroundColor ;

//弹出窗偏移布局

@property (nonatomic, readwrite) UIEdgeInsets popoverLayoutMargins ;

//弹出窗后台视图类

@property (nonatomic, readwrite, retain) Class popoverBackgroundViewClass;

方法:

//创建弹出窗控制器实例,内容控制器为参数

- (instancetype)initWithContentViewController:(UIViewController *)viewController;

//设置弹出窗控制器的内容控制器

- (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated;

//设置弹出窗内容区域的大小

- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;

//弹出窗围绕着某一块特定区域显示(箭头指定那块特定区域)

- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;

//弹出窗围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;

//关闭弹出窗

- (void)dismissPopoverAnimated:(BOOL)animated;

@end

协议:

@protocol UIPopoverControllerDelegate <NSObject>

@optional

//弹出窗将要弹出时触发的方法

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController;

//弹出窗弹出时触发的方法

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

//弹出窗将要复位到某一特定区域时触发的方法

- (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView **)view;

@end

演示实例如下:

第一种方式:弹出窗围绕着某一块特定区域显示(箭头指定那块特定区域),我使用按钮UIButton作为特定区域。

1.首先创建一个内容控制器ContentViewController,它直接继承自UIViewcontroller

2.所有的文件截图为:

3.在内容控制器ContentViewController.m文件使用UIViewController的preferredContentSize属性设置显示区域大小

#import "ContentViewController.h"
@interface ContentViewController ()
@end @implementation ContentViewController
- (void)viewDidLoad {
[super viewDidLoad]; //设置视图颜色
self.view.backgroundColor = [UIColor redColor]; //设置内容控制器显示局域大小
self.preferredContentSize = CGSizeMake(, );
}
@end

4.在当前控制器ViewController.m文件中进行主要代码操作:

//声明属性

#import "ViewController.h"
#import "ContentViewController.h" @interface ViewController ()
@property (strong,nonatomic)UIPopoverController *popoverVC; //声明弹出窗控制器
@property (strong,nonatomic)ContentViewController *contentVC; //声明内容控制器
@end

//创建UIButton按钮控件,并添加打开弹出窗事件

- (void)viewDidLoad {
[super viewDidLoad]; //创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; //设置按钮标题
[button setTitle:@"打开" forState:UIControlStateNormal]; //设置按钮背景颜色
button.backgroundColor = [UIColor purpleColor]; //添加事件,用来打开弹出窗
[button addTarget:self action:@selector(Open:) forControlEvents:UIControlEventTouchUpInside]; //添加按钮到视图中
[self.view addSubview:button];
}

//实现按钮事件

-(void)Open:(UIButton *)sender
{
if (!self.popoverVC.isPopoverVisible)
{
//设置内容控制器
self.contentVC = [[ContentViewController alloc]init]; //创建UIPopoverController(将当前视图控制器设置为内容控制器)
self.popoverVC = [[UIPopoverController alloc]initWithContentViewController:self.contentVC]; //设置一直显示区域,此时点击视图和弹出窗与用户不会发生任何交互,只有按钮能与用户交互
self.popoverVC.passthroughViews = @[self.view,sender]; //打开popoverVC控制器,设置箭头方向为自适应
[self.popoverVC presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
//关闭popoverVC控制器
[self.popoverVC dismissPopoverAnimated:YES];
}
}

//演示结果截图

开始时 :

点击按钮时:(因为设置了passthroughViews,此时,仅有按钮能与用户交互,点击可以关闭弹出窗,点击其他区域没有任何效果)

第二种方式:弹出窗围绕着UIBarButtonItem显示,箭头指定UIBarButtonItem

1.首先创建一个内容控制器ContentViewController,它直接继承自UIViewcontroller

2.所有的文件截图为:

3.在当前控制器ViewController.m文件中进行主要代码操作:

//声明属性

#import "ViewController.h"
#import "ContentViewController.h" @interface ViewController ()
@property (strong,nonatomic)UIPopoverController *popoverVC; //声明弹出窗控制器
@property (strong,nonatomic)ContentViewController *contentVC;//声明内容控制器
@end

//创建UIBarButtonItem,并添加打开弹出窗事件

- (void)viewDidLoad {
[super viewDidLoad]; //创建工具栏控件
UIToolbar *toolBar = [[UIToolbar alloc]init];
toolBar.frame = CGRectMake(, , self.view.frame.size.width,);
//创建工具栏目
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"打开" style:UIBarButtonItemStylePlain target:self action:@selector(Open:)];
UIBarButtonItem *fixItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixItem.width = ; //将工具栏目添加到工具栏
[toolBar setItems:@[fixItem,item]]; //将工具栏添加到控制器视图中
[self.view addSubview:toolBar];
}

//实现按钮事件,其中通过弹出窗的popoverContentSize属性设置弹出窗内容区域的颜色

#pragma mark -打开弹窗
-(void)Open:(UIBarButtonItem *)sender
{
if (!self.popoverVC.popoverVisible)
{
//创建内容控制器
self.contentVC = [[ContentViewController alloc]init]; //创建popoverVC
self.popoverVC = [[UIPopoverController alloc]initWithContentViewController:self.contentVC]; //设置内容显示区域大小和背景颜色
//self.popoverVC.popoverContentSize = CGSizeMake(100, 200);
[self.popoverVC setPopoverContentSize:CGSizeMake(, ) animated:YES];
self.popoverVC.backgroundColor = [UIColor redColor]; //设置一直显示的区域,过滤了当前视图和内容区域,此时只有UIBarButtonItem能与用户进行交互
self.popoverVC.passthroughViews = @[self.view,sender]; //打开弹窗口
[self.popoverVC presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
//关闭弹窗口
[self.popoverVC dismissPopoverAnimated:YES];
}
}

//演示结果截图:

开始时:

点击按钮时:(因为设置了passthroughViews,此时,仅有按钮能与用户交互,点击可以关闭弹出窗,点击其他区域没有任何效果)

iOS:弹出窗控制器:UIPopoverController的更多相关文章

  1. iOS:模态弹出窗控制器UIPopoverPresentationController

    模态弹出窗控制器:UIPopoverPresentationController 实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定 ...

  2. Jquery和Javascript 实际项目中写法基础-弹出窗和弹出层 (4)

     一.实际项目中有很多如下界面效果.    二.该效果可以归结为弹出窗或者弹出层来实现的,为什么这么说?看如下代码:      <!DOCTYPE html> <html> & ...

  3. Bootstrap模态弹出窗

    Bootstrap模态弹出窗有三种方式: 1.href触发模态弹出窗元素: <a class="btn btn-primary" data-toggle="moda ...

  4. jquery Mobile弹出窗

    先创建一个窗体 <div data-role="popup" id="popupView" class="ui-content" da ...

  5. IOS弹出视图 LewPopupViewController

    LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...

  6. easyui弹出窗关闭前调用确认窗口,先关闭页面后调用弹出窗口

    弹出窗关闭的时候提示是否关闭,同时进行一些对应的方法调用, 然而在进行页面关闭调用的时候,往往页面关闭了,才弹出确认对话框, $.messager.confirm和panel的onBeforeClos ...

  7. Android应用之——百度地图最新SDK3.0应用,实现最经常使用的标注覆盖物以及弹出窗覆盖物

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/yanglfree/article/details/33333413 一.概述 最新版的百度地图SDK ...

  8. data-参数说明(模态弹出窗的使用)

    除了通过data-toggle和data-target来控制模态弹出窗之外,Bootstrap框架针对模态弹出框还提供了其他自定义data-属性,来控制模态弹出窗.比如说:是否有灰色背景modal-b ...

  9. jquery ui dialog弹出窗 清空缓存Cache或强制刷新

    我用jquery ui 弹出一个购物车的对话,通过AJAX加载的数据.发现购物车被缓存,一直看到是旧数据.为了刷新购物车更新,我必须去加一个刷新按钮,点击后更新购物车页面.有没有一种方法来自动刷新加载 ...

随机推荐

  1. Windows 7系统下删除开机引导项的方法

    Windows 7系统下删除开机引导项的方法: 1.使用管理员权限运行cmd,在命令行窗口使用  bcdedit 回车 2.查找description为你想删除的段,看对应的标识符是多少, 然后使用 ...

  2. mobiscroll 控件的使用(手机端日历控件)

    先上一张图吧: 控件的下载地址:http://www.kuitao8.com/20140604/2615.shtml 文档API地址:http://docs.mobiscroll.com/2-13-2 ...

  3. Careercup - Microsoft面试题 - 6337018766295040

    2014-05-10 06:38 题目链接 原题: What do you think is the next big thing in technology? For example, search ...

  4. Google Guava学习笔记——基础工具类Joiner的使用

    Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...

  5. Ombrophobic Bovines - POJ 2391

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  6. 随机产生30个两个两位数相加的题目(java)

    编程思路: 1首先遇到JAVA产生随机数的问题. 2把产生的随机数设定范围. 3把划分的范围再分四个小区段分别对应四则运算法则加减乘除. 4打印输出. 题目源代码(Java) package coun ...

  7. HTTP1.1缓存策略

    以下是一幅虽然信息包含量有限.但足够以最简洁的方式说明了“什么是HTTP1.1缓存策略”的图  缓存和缓存策略 web缓存(web cache)或代理缓存(proxy cache)是一种特殊的HTTP ...

  8. E3: PS4/PC 莎木3 众筹200万美元 9小时内达成

    这次E3任天堂没有新掌机(传说中的XDS呢.呵呵)和口袋的消息, 被喷得很严重, 索尼的FF7重制版和莎木3是亮点. 而莎木3的众筹速度据说创了记录, 玩家的情怀大胜. 笔者看到国内也有赞助几十刀的玩 ...

  9. VC++之GetLastError()使用说明

    VC中GetLastError()获取错误信息的使用 在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请 ...

  10. html之colspan && rowspan讲解

    1.colspan && rowspan均在td标签中使用 2.每个单元格大小一致的前提 <table border="1" bordercolor=&quo ...