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.pswp.cn/news/458678.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458678.shtml
英文地址,請注明出處:http://en.pswp.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;