2025-05-24

效果:

 

 

 

 

 

1.對UIPopoverController的簡單概述                 

 

1.1

UIPopoverController是在iPad開發中常用的一個組件(在iPhone上不允許使用),使用非常簡單

 

1.2

UIPopoverController也是一個控制器,跟其他控制器不一樣的是,它直接繼承自NSObject,並非繼承自UIViewController

 

1.3

它隻占用部分屏幕空間來呈現信息,而且顯示在屏幕的最前面,(如上圖所示)

 

 

 

2.UIPopoverController實現                       

 

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

 

2.2設置內容控制器                                         

 

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

 

 

 

設置內容控制器有三種方法:

 

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

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

 

 

 

@property (nonatomic, retain) UIViewController *contentViewController;

通過@property設置內容控制器

 

 

 

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

animated可以指定設置內容控制器時要不要帶有動畫效果

 

復制代碼

1 @interfaceQCLocationButton() <UIPopoverControllerDelegate>

3 {

5     UIPopoverController *_popover;

7 }

復制代碼

 

 

 // 2.彈出popover(默認特性:點擊popover之外的任何地方,popover都會隱藏)

    // 2.1.內容

    QCCityListViewController *cityList = [[QCCityListViewController alloc] init];

    

    // 2.2.將內容塞進popover中

    _popover = [[UIPopoverController alloc] initWithContentViewController:cityList];

 

 

 

 

2.3設置內容的尺寸                                         

 

顯示出來占據多少屏幕空間

 

設置內容的尺寸有兩種方法:

 

@property (nonatomic) CGSize popoverContentSize;

 

 

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

 

 

1  // 2.3.設置popover的內容尺寸

2     _popover.popoverContentSize = CGSizeMake(320, 480);

 

 

 

 

2.4設置顯示的位置                                           

 

從哪個地方冒出來

 

設置顯示的位置有兩種方法:

 

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

這個方法需要傳入一個CGRecView的,也就是說CGRect以View的左上角為坐標原點(0, 0)

 

這個CGRect的值是相對於這個第一種方法是:

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

 

(2)第二種方法是:

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

 

 

 

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

箭頭會指向某一個UIBarButtonItem

 

假如iPad的屏幕發生瞭旋轉,UIPopoverController顯示的位置可能會改變,那麼就需要重寫控制器的某個方法

 

– (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

 

在上面的方法中重寫設置UIPopoverController顯示的位置

 

1     // 2.5.展示popover

2     // self.bounds — self

3     // self.frame  — self.superview

4     [_popover presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *