iOS:弹出窗控制器:UIPopoverController
弹出窗控制器:UIPopoverController
截图:

- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
以上方法和属性都是UIPopoverController的
@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的更多相关文章
- iOS:模态弹出窗控制器UIPopoverPresentationController
模态弹出窗控制器:UIPopoverPresentationController 实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定 ...
- Jquery和Javascript 实际项目中写法基础-弹出窗和弹出层 (4)
一.实际项目中有很多如下界面效果. 二.该效果可以归结为弹出窗或者弹出层来实现的,为什么这么说?看如下代码: <!DOCTYPE html> <html> & ...
- Bootstrap模态弹出窗
Bootstrap模态弹出窗有三种方式: 1.href触发模态弹出窗元素: <a class="btn btn-primary" data-toggle="moda ...
- jquery Mobile弹出窗
先创建一个窗体 <div data-role="popup" id="popupView" class="ui-content" da ...
- IOS弹出视图 LewPopupViewController
LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...
- easyui弹出窗关闭前调用确认窗口,先关闭页面后调用弹出窗口
弹出窗关闭的时候提示是否关闭,同时进行一些对应的方法调用, 然而在进行页面关闭调用的时候,往往页面关闭了,才弹出确认对话框, $.messager.confirm和panel的onBeforeClos ...
- Android应用之——百度地图最新SDK3.0应用,实现最经常使用的标注覆盖物以及弹出窗覆盖物
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/yanglfree/article/details/33333413 一.概述 最新版的百度地图SDK ...
- data-参数说明(模态弹出窗的使用)
除了通过data-toggle和data-target来控制模态弹出窗之外,Bootstrap框架针对模态弹出框还提供了其他自定义data-属性,来控制模态弹出窗.比如说:是否有灰色背景modal-b ...
- jquery ui dialog弹出窗 清空缓存Cache或强制刷新
我用jquery ui 弹出一个购物车的对话,通过AJAX加载的数据.发现购物车被缓存,一直看到是旧数据.为了刷新购物车更新,我必须去加一个刷新按钮,点击后更新购物车页面.有没有一种方法来自动刷新加载 ...
随机推荐
- java静态变量、静态方法和静态代码段
先上实例 public class TestStatic { public static String staticString = "this is a static String&quo ...
- Daily Scrum3
今天我们小组开会内容分为以下部分: part 1: 汇报之前分配的任务进度: part 2:分配明天的任务. ◆Part 1 组员进度报告 彭佟小组完成的优化目标: 关于软件防滥用及垃圾信息拦 ...
- 在Action中以Struts2的方式输出JSON数据
参考地址;http://blog.csdn.net/itdada/article/details/21344985
- .NET-提取字符串实践总结
前阶段工作时遇到一个截取字符串的问题,由于字符串比较长,大概得几万字符吧(XML形式),要提取中间两个节点之间的内容,在网上费了好大功夫才找到能用的正则.工作当用的时候碰到这样的事最蛋疼了,网上的资源 ...
- JavaScript 变量、作用域和内存问题
JavaScript的基本类型值和引用类型值具有以下特点: 1.基本类型值在内存中占据固定的大小,因此被保存在栈内存中: 2.引用类型值是对象,保存在堆内存中: 3.从变量向另一个变量复制基本类型的值 ...
- JS 学习笔记--2--变量的声明
1.ECMAScript 中规定所有的关键字.保留字.函数名.函数名.操作符等都是区分大小写的. 2.标识符:指变量.函数.属性的名字:标识符组成:以字母.下划线.$ 开头,其他字母可以含有数字, ...
- EditPlus配置[C++] [Python] [Java] 编译运行环境
以前一直用Codeblocks写C++,eclipse写Java,再在eclipse里面集成PyDev写Python,首先无法忍受代码自动补全功能(这个功能也许你万分喜欢),也无法忍受如此重量级的ID ...
- 【BZOJ】【1855】【SCOI2010】/【HDOJ】【3401】股票交易
DP/单调队列优化 题解:http://www.cnblogs.com/jianglangcaijin/p/3799736.html 令f[i][j]表示第 i 天结束后,手里剩下 j 股的最大利润, ...
- 用boost共享内存实现进程通信的例子
发送端 #include "DBProc1.h" #include <string> #include <thread> #include <boos ...
- nginx上如何支持.htaccess伪静态转向
我们知道在apache上有一个常用的功能.htaccess转向,只要apache编译的时候指明支持rewrite模块就可以了. 但是换到nginx上方法会有一点不一样,网上很多人说把.htaccess ...