zoukankan      html  css  js  c++  java
  • UIPopoverController简单实用

    一:简介

    •UIPopoverController是在iPad开发中常用的一个组件(在iPhone上不允许使用),使用非常简单
    •UIPopoverController也是一个控制器,跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController
    •它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面

    二:使用步骤

    要想成功显示一个UIPopoverController,需要经过下列步骤:

    1>设置内容控制器

        由于UIPopoverController直接继承自NSObject,不具备可视化的能力,因此UIPopoverController上面的内容必须由另外一个继承自UIViewController的控制器来提供,这个称为“内容控制器”

    设置内容控制器有三种方法:
    - (id)initWithContentViewController:(UIViewController *)viewController;
    在初始化UIPopoverController的时候传入一个内容控制器
    
    @property (nonatomic, retain) UIViewController *contentViewController;
    通过@property设置内容控制器
    
    - (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated;
       animated可以指定设置内容控制器时要不要带有动画效果
    2>设置内容的尺寸
         显示出来占据多少屏幕空间
    设置内容的尺寸有两种方法:
    @property (nonatomic) CGSize popoverContentSize;
    
    - (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
    3>设置显示的位置

        从哪个地方冒出来

    设置显示的位置有两种方法:
    - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
    这个方法需要传入一个CGRect和一个View,用来设置箭头指向的位置
    这个CGRect的值是相对于这个View的,也就是说CGRect以View的左上角为坐标原点(0, 0)
    
    举个例子,如果你要箭头指向被点击的button,那么
    第一种方法是:
    [pop presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    第二种方法是:
    [pop presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    
    - (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
    箭头会指向某一个UIBarButtonItem

    三:使用注意

    1>假如iPad的屏幕发生了旋转,UIPopoverController显示的位置可能会改变,那么就需要重写控制器的某个方法

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

    在上面的方法中重写设置UIPopoverController显示的位置

    2>UIPopoverController和UIActionSheet有点不一样,需要自己管理内存

    u必须retain这个UIPopoverController对象
    u在ARC中,也就是用一个强引用来引用它
    •在释放(调用dealloc)UIPopoverController对象之前一定要先dismiss
    •在ARC下UIPopoverController的正确用法
    (1)用一个强引用(成员变量或strong的@property)保存UIPopoverController对象

    _pop = [[UIPopoverController alloc] initWithContentViewController:vc];

    (2)设置UIPopoverController的delegate
    在- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController代理方法中将强引用清空

    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

    {

        _pop = nil;

    }

    3>防止点击UIPopoverController区域外消失

    •默认情况下,只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的,点击UIPopoverController区域外的控件后,UIPopoverController会默认消失
    •要想点击UIPopoverController区域外的控件时UIPopoverController不消失,解决办法是设置UIPopoverController的passthroughViews属性

    @property (nonatomic, copy) NSArray *passthroughViews;

    这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了

    4>外观

    有2个属性可以微调UIPopoverController的外观

    @property (nonatomic, readwrite) UIEdgeInsets popoverLayoutMargins

    这个属性可以调整显示的位置,设置跟默认显示位置的间距,比如UIEdgeInsetsMake(0, 0, 0, 40)代表距离默认显示位置有40的间距

    @property (nonatomic, readwrite, retain) Class popoverBackgroundViewClass

    这个属性可以修改UIPopoverController的背景样式,需要设置一个继承了UIPopoverBackgroundView的子类类名,子类通过实现相应的方法来修改外观

    四:其它属性与方法

    @property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;
    UIPopoverController的代理
    
    @property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
    UIPopoverController是否可见
    
    @property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
    箭头的方向
    
    - (void)dismissPopoverAnimated:(BOOL)animated;
    关闭UIPopoverController,会触发相应的代理方法
  • 相关阅读:
    Python基础(14)_python模块之configparser模块、suprocess
    Python基础(13)_python模块之re模块(正则表达式)
    Python基础(12)_python模块之sys模块、logging模块、序列化json模块、pickle模块、shelve模块
    Python基础(11)_python模块之time模块、rando模块、hashlib、os模块
    Python基础(10)_内置函数、匿名函数、递归
    python中内建函数isinstance的用法
    Python基础(9)_生成器(yield表达式形式)、面向过程编程
    Python基础(8)_迭代器、生成器、列表解析
    Python基础(7)_闭包函数、装饰器
    Python基础(6)_函数
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3891788.html
Copyright © 2011-2022 走看看