前言
學(xué)習(xí)的時(shí)候遺漏了這個(gè)知識(shí)點(diǎn),最近在做一個(gè)類(lèi)微信牙瓢、支付寶的 "+"號(hào)彈出浮窗的功能,發(fā)現(xiàn)了這個(gè)好用的東西~ 做個(gè)記錄方便日后查閱
NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED //iOS8之后可用 @interface UIPopoverPresentationController : UIPresentationController
基本使用
效果圖
點(diǎn)擊pop按鈕后觸發(fā)的Action代碼如下
/* Present the view controller using the popover style. */
// 每個(gè)viewController,都有一個(gè)modalPresentationStyle屬性
TestViewController * test = [[TestViewController alloc]init];
test.preferredContentSize = CGSizeMake(300, 200);//設(shè)置浮窗的寬高
test.modalPresentationStyle = UIModalPresentationPopover;
/* Get the popover presentation controller and configure it. */
//獲取TestViewController的UIPopoverPresentationController
UIPopoverPresentationController * popover = [test popoverPresentationController];
popover.delegate = self;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;//設(shè)置箭頭位置
popover.sourceView = self.popViewButton;//設(shè)置目標(biāo)視圖
popover.sourceRect = self.popViewButton.bounds;//彈出視圖顯示位置
popover.backgroundColor = [UIColor redColor];//設(shè)置彈窗背景顏色(效果圖里紅色區(qū)域)
[self presentViewController:test animated:YES completion:nil];
** UIPopoverPresentationControllerDelegate代理方法 **
/*
For iOS8.0, the only supported adaptive presentation styles
are UIModalPresentationFullScreen and UIModalPresentationOverFullScreen. */
// 設(shè)置 浮窗彈窗的推出樣式床三,效果圖中設(shè)置style為UIModalPresentationNone
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:
(UIPresentationController *)controller;
// Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the
// dismissal of the view.
// 點(diǎn)擊浮窗背景popover controller是否消失
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
// Called on the delegate when the user has taken action to dismiss the popover. This is not called when the popover is dimissed programatically.
// 浮窗消失時(shí)調(diào)用
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
防止點(diǎn)擊UIPopoverController區(qū)域外消失
默認(rèn)情況下
只要UIPopoverController顯示在屏幕上一罩,UIPopoverController背后的所有控件默認(rèn)是不能跟用戶(hù)進(jìn)行正常交互的
點(diǎn)擊UIPopoverController區(qū)域外的控件,UIPopoverController默認(rèn)會(huì)消失
要想點(diǎn)擊UIPopoverController區(qū)域外的控件時(shí)不讓UIPopoverController消失撇簿,解決辦法是設(shè)置passthroughViews屬性
@property (nonatomic, copy) NSArray *passthroughViews;
這個(gè)屬性是設(shè)置當(dāng)UIPopoverController顯示出來(lái)時(shí)聂渊,哪些控件可以繼續(xù)跟用戶(hù)進(jìn)行正常交互。這樣的話四瘫,點(diǎn)擊區(qū)域外的控件就不會(huì)讓UIPopoverController消失了
寫(xiě)在最后
官網(wǎng)對(duì)UIPopoverPresentationController介紹
Human Interface Guidelines——Popovers