popoverController(iPad)

一、设置尺寸

提示:不建议,像下面这样吧popover的宽度和高度写死。

复制代码
 1 //1.新建一个内容控制器2     YYMenuViewController *menuVc=[[YYMenuViewController alloc]init];3     4     //2.新建一个popoverController,并设置其内容控制器5     self.popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];6     7     //3.设置尺寸8     self.popover.popoverContentSize=CGSizeMake(300, 200);9     
10     //4.显示
11     [self.popover presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
复制代码

更好的设计是:popover的尺寸应该由内部控制器的内容所决定。

从iOS 7开始  @property (nonatomic) CGSize preferredContentSize;

  该属性是UIViewController的

复制代码
 1 -(NSArray *)menus2 {3     if (_menus==nil) {4         _menus=@[@"列表1",@"列表2",@"列表3",@"列表4",@"列表4",@"列表4",@"列表4",@"列表4",@"列表4",@"列表4",@"列表1",@"列表2",@"列表1",@"列表2"];5     }6  return _menus;7 }8 - (void)viewDidLoad9 {
10     [super viewDidLoad];
11     
12     //设置控制器将来在popover中的尺寸
13     CGFloat maxH=MIN(480,self.menus.count*44);
14     //ios7以前的设置
15 //    self.contentSizeForViewInPopover=CGSizeMake(150, maxH);
16     //ios7以后
17     self.preferredContentSize=CGSizeMake(150, maxH);
18     
19 }
复制代码

效果:

  

关于MIN(A,B)的说明,最终的大小取决于B,但是最大不能超过A,如果超过A那么值就等于A。

 

二、设置显示的位置

1.设置显示的位置有2种方法

(1)围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)

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

item :围绕着哪个UIBarButtonItem显示

arrowDirections :箭头的方向

animated :是否通过动画显示出来

 

(2)围绕着某一块特定区域显示(箭头指定那块特定区域)

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

rect :指定箭头所指区域的矩形框范围(位置和尺寸),以view的左上角为坐标原点

view :rect参数是以view的左上角为坐标原点(0,0)

arrowDirections :箭头的方向

animated :是否通过动画显示出来

rect和view参数如下:

相关代码:

复制代码
 1 //2 //  YYViewController.m3 //  01-PopoverController简单介绍4 //5 //  Created by apple on 14-8-17.6 //  Copyright (c) 2014年 yangyong. All rights reserved.7 //8 9 #import "YYViewController.h"
10 #import "YYMenuViewController.h"
11 
12 @interface YYViewController ()<UIPopoverControllerDelegate>
13 @property(nonatomic,strong)UIPopoverController *popover;
14 - (IBAction)buttonClick:(UIButton *)sender;
15 @end
16 
17 @implementation YYViewController
18 
19 - (void)viewDidLoad
20 {
21     [super viewDidLoad];
22 }
23 
24 -(void)showPopoverFromItem
25 {
26     //1.新建一个内容控制器
27     YYMenuViewController *menuVc=[[YYMenuViewController alloc]init];
28     
29     //2.新建一个popoverController,并设置其内容控制器
30     self.popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];
31     
32     //3.设置尺寸
33     //    self.popover.popoverContentSize=CGSizeMake(300, 200);
34     
35     //4.显示
36     [self.popover presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
37     
38     //5.设置代理
39     self.popover.delegate=self;
40 }
41 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
42 {
43     
44 }
45 
46 #pragma mark-代理方法
47 //popoverController消失的时候调用
48 -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
49 {
50 }
51 //popoverController的位置改变的时候调用(如竖屏变横屏)
52 -(void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view
53 {
54     
55 }
56 //用来决定用户点击了蒙版后,popoverController是否可以dismiss,返回YES代表可以,返回NO代表不可以
57 -(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
58 {
59     return NO;
60 }
61 - (IBAction)buttonClick:(UIButton *)sender {
62     
63     //1.新建一个popoverController并设置其内容控制器
64     YYMenuViewController *menuVc=[[YYMenuViewController alloc]init];
65     self.popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];
66     
67     //2.显示
68     //2.1第一种方式
69 //    [self.popover presentPopoverFromBarButtonItem:<#(UIBarButtonItem *)#> permittedArrowDirections:<#(UIPopoverArrowDirection)#> animated:<#(BOOL)#>];
70     //2.2第二种方式
71     [self.popover presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
72     //说明:popover会指向sender.bounds这一块矩形框,这块矩形框以sender的左上角为坐标原点
73     //注意:注意sender.frame和sender.bounds的区别
74     
75 }
76 @end
复制代码

界面效果:(部分)

  

关于frame坐标计算的图示:

      

下面两者是等价的:

  

即如果想让箭头指向某一个UIView的做法有2种做法,比如指向一个button

方法1

  [popover presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

方法2

  [popover presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

 

三、设置代理

代理对象

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

是否可见

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

箭头方向

  @property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection; 

关闭popover(让popover消失)

  - (void)dismissPopoverAnimated:(BOOL)animated;

代码说明:

复制代码
 1   .......2   //5.设置代理3     self.popover.delegate=self;4 }5 6 #pragma mark-代理方法7 //popoverController消失的时候调用8 -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController9 {
10 }
11 //popoverController的位置改变的时候调用(如竖屏变横屏)
12 -(void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view
13 {
14     
15 }
16 //用来决定用户点击了蒙版后,popoverController是否可以dismiss,返回YES代表可以,返回NO代表不可以
17 -(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
18 {
19     return NO;
20 }
复制代码

四、防止点击UIPopoverController区域外消失

默认情况下

只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的

点击UIPopoverController区域外的控件,UIPopoverController默认会消失

 

要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性

@property (nonatomic, copy) NSArray *passthroughViews;

这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了

代码示例:

复制代码
 1 - (IBAction)buttonClick:(UIButton *)sender {2     3     //1.新建一个popoverController并设置其内容控制器4     YYMenuViewController *menuVc=[[YYMenuViewController alloc]init];5     self.popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];6     7     //设置过滤掉一些控件8     self.popover.passthroughViews=@[self.switchview];9     
10     //2.显示
11     //2.1第一种方式
12 //    [self.popover presentPopoverFromBarButtonItem:<#(UIBarButtonItem *)#> permittedArrowDirections:<#(UIPopoverArrowDirection)#> animated:<#(BOOL)#>];
13     //2.2第二种方式
14 //    [self.popover presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
15     [self.popover presentPopoverFromRect:sender.frame inView:sender.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
16     //说明:popover会指向sender.bounds这一块矩形框,这块矩形框以sender的左上角为坐标原点
17     //注意:注意sender.frame和sender.bounds的区别
18     
19 }
复制代码

补充:

UIPopoverController这个类是只能用在iPad中的

要想在iPhone中实现popover效果,必须得自定义view,可以参考

http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000

http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000

转载于:https://www.cnblogs.com/yintingting/p/4955899.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/458678.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

静态成员变量和非静态成员变量的对比

静态成员变量和非静态成员变量的对比 1、存储的数据 静态成员变量存储的是所有对象共享的数据 非静态成员变量存储的是每个对象特有的数据 2、存储位置 静态成员变量是随着类的加载在方法区的静态区开辟内存了 非静态成员变量是随着对象的创建再堆中开辟内存 3、调用方式 静态成…

c++的thread类(c++线程简单用法)

最近看了一个Thread类&#xff08;忘记在哪里看的了&#xff09;&#xff0c;感觉不错。 创建线程时线程对应的函数必须是类的静态成员&#xff0c;由于静态成员无法访问类的非静态成员&#xff0c;我从前都是把对象的指针作为参数传递给线程函数来避免这个问题&#xff0c;但是…

[LeetCode]Merge Sorted Array

题目描述:(链接) Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m n) to hold additional elements from nums2. The number of eleme…

[LeetCode]Integer to Roman

题目描述:(链接&#xff09; Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 解题思路&#xff1a; 1 class Solution {2 public:3 string intToRoman(int num) {4 vector<int> values{1000…

[c++]代理对象模式

代理对象 <code class"hljs cpp has-numbering" style"display: block; padding: 0px; box-sizing: border-box; font-family: Source Code Pro, monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius:…

this static 面向对象三大特点

面向对象三大特点&#xff1a;封装、继承、多态 封装&#xff1a;只对外界提供有用的属性和行为 this&#xff1a;是一个引用&#xff0c;总是指向当前对象 static 存放位置是方法区中的静态区 static特点 static修饰的成员变量随着类的加载就在静态区中开辟内存 所…

fastQC

Fastqc用途 FastQC aims to provide a simple way to do some quality control checks on raw sequence data coming from high throughput sequencing pipelines. It provides a modular set of analyses which you can use to give a quick impression of whether your data …

C++代理 Surrogate

容器通常只能包含一种类型的对象&#xff0c;所以很难在容器中存储对象本身。存储指向对象的指针&#xff0c;虽然允许通过继承来处理类型不同的问题&#xff08; 多态性 &#xff09;&#xff0c;但是也增加了内存分配的额外负担。所以我们通过定义名为 代理 的对象来解决该问…

C++ Handle(句柄) part1

本文是我学习C&#xff0b;&#xff0b;沉思录第6章的笔记 本文主要讲述了Handle类的概念&#xff0c;定义方法以及写时复制技术。 在前文(Surrogate代理类)的讲解中我们了解到了代理的实现方法. 代理类有很多好处,但是麻烦的是每次都得进行复制.如果该类是经常使用并且member很…

sscanf的高级用法

sscanf的高级用法&#xff08;总结&#xff09; 2012-04-25 18:50:25分类&#xff1a; C/C sscanf(recvbuf,"%*[^/]/%[^ ]s",buf_rev); sscanf(buf, "GET /%[^ ]", buf_rev);这个是在一个webserver.c里面的例子&#xff0c;通过sscanf&#xff08;&#xf…

选择排序 冒泡排序 二分查找

选择排序 int [] arr {2,48,28,32,90,12}; for&#xff08;int i 0&#xff1b; i < arr.length - 1;i&#xff09;{ for(int j i 1; j < arr.length;j){ if(arr[i] < arr[j]){ int c; c arr[i]; arr[i] arr[j]; arr[j] c; } } } 冒泡排序 for(int i 0;i <…

C++, ID、指针、handle (void *)的区别

原文链接&#xff1a; http://hi.baidu.com/dandanfeng160/blog/item/4eaa3df5215bc42dbd310955.html 在Windows程序设计中&#xff0c;句柄是无法精确定义的术语。随便找一个高手&#xff0c;让他给你讲讲句柄是什么&#xff0c;恐怕他都很难给你一个具体的定义来。 在Wind…

Swift调用Objective C的FrameWork

很多Github的库经过很多年的发展&#xff0c;源码都是OC写的&#xff0c;&#xff0c;所以&#xff0c;用Swift调用OC的库就是开发中难免遇到的的一个问题&#xff0c;本文以AFNetworking为例&#xff0c;讲解如何跨语言调用。 第一步 创建一个空的工程 注意&#xff0c;语言选…

命令行 java文本编辑工具 重载 内存区域 栈 堆

一、dir 列出当前目录下的文件以及文件夹 md创建目录 rd删除目录 cd 进入指定目录 cd..返回到上一级目录 &#xff1a; 切换盘符 比如&#xff1a; F: 二、editPlus 编写程序 三、重载&#xff1a;在同一个class中&#xff0c;出现了函数名称相同&#xff0…

数据结构(Java)——查找和排序(1)

1.查找的定义 查找是这样一个过程&#xff0c;即在某个项目组中寻找某一指定目标元素&#xff0c;或者确定该组中并不存在该目标元素。 对其进行查找的项目的组有时也成为查找池。两种常见的查找方式&#xff1a;线性查找和二分查找。为了能够查找某一对象&#xff0c;我们就必…

GetProcAddress()用法

函数功能描述: GetProcAddress()函数检索指定的动态链接库(DLL)中的输出库函数地址。 函数原型&#xff1a; FARPROC GetProcAddress( HMODULE hModule, // DLL模块句柄 LPCSTR lpProcName // 函数名 ); 参数&#xff1a; hModule [in] 包含此函数的…

支付宝问题LaunchServices: ERROR: There is no registered handler for URL scheme alipay

LaunchServices: ERROR: There is no registered handler for URL scheme alipay &#xff08;这句话其实是在告诉你 设备上没有安装 支付宝的客户端,此时会走网页端&#xff09;而有人会发现并没有HTML5网页弹出过一会&#xff0c;会发现服务器返回4000支付失败&#xff0c;这…

C++string类常用函数 c++中的string常用函数用法总结

string类的构造函数&#xff1a; string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外&#xff0c;string类还支持默认构造函数和复制构造函数&#xff0c;如string s1&#xff1b;string s2"hello"&#xff1b;都是正…

排列与组合

话说&#xff0c;初一的时候看到这样一道题&#xff1a;有一种彩票中奖率为1%&#xff0c;买一百张是不是一定能中奖&#xff1f;答案自然是否定的&#xff0c;但我在想&#xff0c;如果有200张彩票&#xff0c;两张有奖&#xff0c;买一百张中奖率是多少&#xff1f;一天晚上睡…

剔除服务器返回的NSNull格式的数据

服务器返回NSNull格式的数据&#xff0c;真。。的烦人 解决办法&#xff1a;在AFN请求里面加上下面两段代码&#xff0c;OK AFJSONResponseSerializer *response (AFJSONResponseSerializer *)manager.responseSerializer; response.removesKeysWithNullValues YES;