iOS開發之UIPopoverController

來源:互聯網
上載者:User

標籤:

1、概述

是iPad開發中常見的一種控制器(在iPhone上不允許使用),跟其他控制器不一樣的是,它直接繼承自NSObject,並非繼承自UIViewController,它只佔用部分螢幕空間來呈現資訊,而且顯示在螢幕的最前面。

2使用步驟

要想顯示一個UIPopoverController,需要經過下列步驟:

第一步設定內容控制器

由於UIPopoverController直接繼承自NSObject不具備可視化的能力。因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供,這個控制器稱為“內容控制器”。

設定內容控制器有3種方法:

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

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

(2)@property (nonatomic, retain) UIViewController

*contentViewController;

(3)- (void)setContentViewController:

(UIViewController *)viewController animated:(BOOL)animated;

以上方法和屬性都是UIPopoverController的。

第二步:設定內容的尺寸

即設定顯示出來佔據多少螢幕空間。

設定內容的尺寸有2種方法:

(1)@property (nonatomic) CGSize popoverContentSize;

(2)- (void)setPopoverContentSize:(CGSize)size animated:

(BOOL)animated;

以上方法和屬性都是UIPopoverController的。

也可以在UIPopoverController的ContentViewController(內容控制器)的viewDidLoad方法中設定popoverContentSize來設定其顯示在UIPopoverController中的大小。

第三步:設定顯示的位置

即設定從哪個地方冒出來。

設定顯示的位置有2種方法:

(1)圍繞著一個UIBarButtonItem顯示(箭頭指定那個UIBarButtonItem)

- (void)PopoverFromBarButtonItem:(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 :rect參數是以view的左上方為座標原點(0,0)

arrowDirections :箭頭的方向

animated :是否通過動畫顯示出來

3rectview參數

 

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];

 

5常見報錯

在popover的使用過程中,經常會遇到這個錯誤:

-[UIPopoverController dealloc] reached while popover is still visible.

錯誤的大體意思是:popover在仍舊可見的時候被銷毀了(調用了dealloc)。

從錯誤可以得出的結論:

(1)當popover仍舊可見的時候,不準銷毀popover對象

(2)在銷毀popover對象之前,一定先讓popover消失(不可見)

例如:

-(IBAction)menuClick:(UIBarButtonItem *)sender {

//建立內容控制器

UIViewController *vc = [[UIVeiwController alloc] init];

vc.view.backgroundColor = [UIColor redColor];

UINavigationController *nav = [[UINavigationController alloc] initWithRootController:vc];

//建立popover

UIPopoverController *titlePopover = [[UIPopoverController alloc] initWithContentViewController:nav];

//可以不設定尺寸,會有一個預設的尺寸320x480

//設定顯示到那個位置

[titlePopover presentPopoverFromBarButtonItem:sender permittedArrowDirectionAny animated:YES];

}

上面代碼就會報這個錯誤,因為在ARC模式下,當執行到最後一句時,titlePopover就會被系統回收,但此時titlePopover仍然顯示在螢幕上,所以會報上面錯誤。解決辦法是建立一個屬性:

@property (nonatomic, strong) UIPopoverController *titlePopover;

把上面代碼紅色部分全部改成self.titlePopover

6、通過內容控制器設定內容尺寸

內容控制器可以自行設定自己在popover中顯示的尺寸:

在iOS 7之前:

@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;

從iOS 7開始:

@property (nonatomic) CGSize preferredContentSize;

以上屬性都是UIViewController的

7常用屬性

代理對象:

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

 

是否可見:

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

 

箭頭方向:

@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;

 

關閉popover(讓popover消失):

- (void)dismissPopoverAnimated:(BOOL)animated;

8防止點擊UIPopoverController地區外消失

預設情況下:

(1)只要UIPopoverController顯示在螢幕上,UIPopoverController背後的所有控制項預設是不能跟使用者進行正常互動的

(2)點擊UIPopoverController地區外的控制項,UIPopoverController預設會消失

要想點擊UIPopoverController地區外的控制項時不讓UIPopoverController消失,解決辦法是設定passthroughViews屬性:

@property (nonatomic, copy) NSArray *passthroughViews;

這個屬性是設定當UIPopoverController顯示出來時,哪些控制項可以繼續跟使用者進行正常互動。這樣的話,點擊地區外的控制項就不會讓UIPopoverController消失了

7、         如何iPhone中實現popover的效果

UIPopoverController這個類是只能用在iPad中的,要想在iPhone中實現popover效果,必須得自訂view,可以參考:

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

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

iOS開發之UIPopoverController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.