UIPopoverController的使用

一闪水、什么是UIPopoverController

是iPad開發(fā)中常見的一種控制器
跟其他控制器不一樣的是,它直接繼承自NSObject孕索,并非繼承自UIViewController
它只占用部分屏幕空間來呈現(xiàn)信息澎办,而且顯示在屏幕的最前面

二、使用步驟

1.要想顯示一個UIPopoverController煌茬,需要經(jīng)過下列步驟
設置內容控制器
由于UIPopoverController直接繼承自NSObject,不具備可視化的能力
因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供彻桃,這個控制器稱為“內容控制器”

2.設置內容的尺寸
顯示出來占據(jù)多少屏幕空間

3.設置顯示的位置
從哪個地方冒出來

三坛善、設置內容控制器有3種方法

在初始化UIPopoverController的時候傳入一個內容控制器

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

@property (nonatomic, retain) UIViewController *contentViewController;

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

以上方法和屬性都是UIPopoverController的

四、設置內容的尺寸

設置內容的尺寸有2種方法

@property (nonatomic) CGSize popoverContentSize;

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

以上方法和屬性都是UIPopoverController的

五邻眷、設置顯示的位置

設置顯示的位置有2種方法
1.圍繞著一個UIBarButtonItem顯示(箭頭指定那個UIBarButtonItem)

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
item :圍繞著哪個UIBarButtonItem顯示
arrowDirections :箭頭的方向
animated :是否通過動畫顯示出來

2.圍繞著某一塊特定區(qū)域顯示(箭頭指定那塊特定區(qū)域)

  • (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
    rect :指定箭頭所指區(qū)域的矩形框范圍(位置和尺寸)
    view :rect參數(shù)是以view的左上角為坐標原點(0眠屎,0)
    arrowDirections :箭頭的方向
    animated :是否通過動畫顯示出來
    3.rect和view參數(shù)
屏幕快照 2017-06-12 上午7.21.53.png

4.如果想讓箭頭指向某一個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];

六肆饶、常見報錯

1.在popover的使用過程中改衩,經(jīng)常會遇到這個錯誤
-[UIPopoverController dealloc] reached while popover is still visible.
錯誤的大體意思是:popover在仍舊可見的時候被銷毀了(調用了dealloc)

從錯誤可以得出的結論
當popover仍舊可見的時候,不準銷毀popover對象
在銷毀popover對象之前驯镊,一定先讓popover消失(不可見)

七葫督、通過內容控制器設置內容尺寸

內容控制器可以自行設置自己在popover中顯示的尺寸
在iOS 7之前
@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;

從iOS 7開始
@property (nonatomic) CGSize preferredContentSize;
以上屬性都是UIViewController的

八竭鞍、常用屬性

代理對象
@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;

是否可見
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;

箭頭方向
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;

關閉popover(讓popover消失)
- (void)dismissPopoverAnimated:(BOOL)animated;

九、防止點擊UIPopoverController區(qū)域外消失

默認情況下
只要UIPopoverController顯示在屏幕上橄镜,UIPopoverController背后的所有控件默認是不能跟用戶進行正常交互的
點擊UIPopoverController區(qū)域外的控件偎快,UIPopoverController默認會消失

要想點擊UIPopoverController區(qū)域外的控件時不讓UIPopoverController消失,解決辦法是設置passthroughViews屬性

@property (nonatomic, copy) NSArray *passthroughViews;

這個屬性是設置當UIPopoverController顯示出來時洽胶,哪些控件可以繼續(xù)跟用戶進行正常交互晒夹。這樣的話,點擊區(qū)域外的控件就不會讓UIPopoverController消失了

十妖异、如何iPhone中實現(xiàn)popover的效果

1.UIPopoverController這個類是只能用在iPad中的

2.要想在iPhone中實現(xiàn)popover效果惋戏,必須得自定義view,可以參考
http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000
http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000

十一他膳、代碼示例

#import "ViewController.h"
#import "MenuViewController.h"
#import "OneViewController.h"
#import "ColorViewController.h"
#import "TestViewController.h"

@interface ViewController () <UIPopoverControllerDelegate>

- (IBAction)menuClick:(UIBarButtonItem *)sender;
- (IBAction)controllerClick:(UIBarButtonItem *)sender;
- (IBAction)selectColor:(UIButton *)sender;
- (IBAction)iOS8:(UIButton *)sender;

/** 菜單的Popover */
@property (nonatomic, strong) UIPopoverController *menuPopover;
/** 控制器的Popover */
@property (nonatomic, strong) UIPopoverController *controllerPopover;
/** 選擇顏色的Popover */
@property (nonatomic, strong) UIPopoverController *colorPopover;

/** iOS的測試Popover */
@property (nonatomic, strong) TestViewController *testVc;
@property (weak, nonatomic) IBOutlet UIButton *testButton;

@end

@implementation ViewController

#pragma mark - 事件的點擊
- (IBAction)menuClick:(UIBarButtonItem *)sender {
    
    // 0.創(chuàng)建內容控制器
    MenuViewController *menuVc = [[MenuViewController alloc] init];
    
    // 1.創(chuàng)建UIPopoverController
    UIPopoverController *menuPopover = [[UIPopoverController alloc] initWithContentViewController:menuVc];
    
    // 2.設置內容控制器
    // [menuPopover setContentViewController:menuVc];
    
    // 3.設置尺寸
    // [menuPopover setPopoverContentSize:CGSizeMake(100, 44 * 3)];
    
    // 4.設置顯示的位置
    [menuPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    // [menuPopover dismissPopoverAnimated:YES];
     
    
    [self.menuPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (IBAction)controllerClick:(UIBarButtonItem *)sender {
    [self.controllerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (IBAction)selectColor:(UIButton *)sender {
    [self.colorPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (IBAction)iOS8:(UIButton *)sender {
    /*
    // 1.創(chuàng)建準備以Popover形式彈出的控制器
    TestViewController *testVc = [[TestViewController alloc] init];
    
    // 2.設置彈出樣式
    testVc.modalPresentationStyle = UIModalPresentationPopover;
    
    // 3.設置在什么位置彈出
    testVc.popoverPresentationController.sourceView = sender;
    testVc.popoverPresentationController.sourceRect = sender.bounds;
    */
    self.testVc.popoverPresentationController.sourceView = sender;
    self.testVc.popoverPresentationController.sourceRect = sender.bounds;
    [self presentViewController:self.testVc animated:YES completion:nil];
}

#pragma mark - 懶加載代碼
- (UIPopoverController *)menuPopover
{
    if (_menuPopover == nil) {
        // 1.創(chuàng)建內容控制器
        MenuViewController *menuVc = [[MenuViewController alloc] init];
        
        // 2.創(chuàng)建UIPopoverController,并且指定內容控制器
        self.menuPopover = [[UIPopoverController alloc] initWithContentViewController:menuVc];
        
        self.menuPopover.delegate = self;
    }
    return _menuPopover;
}

- (UIPopoverController *)controllerPopover
{
    if (_controllerPopover == nil) {
        // 1.創(chuàng)建內容控制器
        OneViewController *oneVc = [[OneViewController alloc] init];
        UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneVc];
        
        // 2.創(chuàng)建UIPopoverController,并且設置內容控制器
        _controllerPopover = [[UIPopoverController alloc] initWithContentViewController:oneNav];
    }
    return _controllerPopover;
}

- (UIPopoverController *)colorPopover
{
    if (_colorPopover == nil) {
        // 1.創(chuàng)建內容控制器
        ColorViewController *colorVc = [[ColorViewController alloc] init];
        
        // 2.創(chuàng)建UIPopoverController,并且指定內容控制器
        self.colorPopover = [[UIPopoverController alloc] initWithContentViewController:colorVc];
        
        __weak UIPopoverController *colorPover = _colorPopover;
        __weak UIView *weakView = self.view;
        colorVc.selectColorBlock = ^(UIColor *color) {
            weakView.backgroundColor = color;
            [colorPover dismissPopoverAnimated:YES];
        };
    }
    return _colorPopover;
}

- (TestViewController *)testVc
{
    if (_testVc == nil) {
        // 1.創(chuàng)建控制器
        _testVc = [[TestViewController alloc] init];
        
        // 2.設置樣式
        self.testVc.modalPresentationStyle = UIModalPresentationPopover;
    }
    
    return _testVc;
}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末响逢,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子棕孙,更是在濱河造成了極大的恐慌舔亭,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蟀俊,死亡現(xiàn)場離奇詭異钦铺,居然都是意外死亡,警方通過查閱死者的電腦和手機肢预,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門矛洞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人烫映,你說我怎么就攤上這事沼本。” “怎么了锭沟?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵抽兆,是天一觀的道長。 經(jīng)常有香客問我族淮,道長辫红,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任祝辣,我火速辦了婚禮贴妻,結果婚禮上,老公的妹妹穿的比我還像新娘较幌。我一直安慰自己揍瑟,他們只是感情好,可當我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布乍炉。 她就那樣靜靜地躺著绢片,像睡著了一般。 火紅的嫁衣襯著肌膚如雪岛琼。 梳的紋絲不亂的頭發(fā)上底循,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機與錄音槐瑞,去河邊找鬼熙涤。 笑死,一個胖子當著我的面吹牛困檩,可吹牛的內容都是我干的祠挫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼悼沿,長吁一口氣:“原來是場噩夢啊……” “哼等舔!你這毒婦竟也來了?” 一聲冷哼從身側響起糟趾,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤慌植,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后义郑,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝶柿,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年非驮,在試婚紗的時候發(fā)現(xiàn)自己被綠了交汤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡劫笙,死狀恐怖芙扎,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情邀摆,我是刑警寧澤纵顾,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站栋盹,受9級特大地震影響施逾,放射性物質發(fā)生泄漏。R本人自食惡果不足惜例获,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一汉额、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧榨汤,春花似錦蠕搜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽轨蛤。三九已至,卻和暖如春虫埂,著一層夾襖步出監(jiān)牢的瞬間祥山,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工掉伏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留缝呕,地道東北人。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓斧散,卻偏偏與公主長得像供常,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鸡捐,可洞房花燭夜當晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內容

  • 1栈暇、什么是UIPopoverController 是iPad開發(fā)中常見的一種控制器(在iPhone上不允許使用,現(xiàn)...
    光明程輝閱讀 10,658評論 5 13
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫闯参、插件瞻鹏、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,121評論 4 61
  • 最近一段時間,安排了每天的三只青蛙馏慨。但還是感覺自己時間不夠用埂淮。什么都想學,想學ppt写隶,想學古琴倔撞,想學攝影,想看書充...
    13組223魏曉花閱讀 205評論 0 0
  • 1.感恩今晚的聚餐慕趴,這些人都是從陌生到相識痪蝇,再到和親人一樣,喜歡這種感覺冕房,喜歡做好吃的大家一起分享躏啰,謝謝你們,我愛...
    明景靈燕閱讀 249評論 0 0