• UIPopoverController的使用


    什么是UIPopoverController
    是iPad开发中常见的一种控制器
    跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController
    它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面

    要想显示一个UIPopoverController,需要经过下列步骤
    设置内容控制器
    由于UIPopoverController直接继承自NSObject,不具备可视化的能力
    因此UIPopoverController上面的内容必须由另外一个继承自UIViewController的控制器来提供,这个控制器称为“内容控制器”

    设置内容的尺寸
    显示出来占据多少屏幕空间

    设置显示的位置
    从哪个地方冒出来

    设置内容控制器

    设置内容控制器有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种方法
    围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)
    - (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
    item :围绕着哪个UIBarButtonItem显示
    arrowDirections :箭头的方向
    animated :是否通过动画显示出来
    
    围绕着某一块特定区域显示(箭头指定那块特定区域)
    - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
    rect :指定箭头所指区域的矩形框范围(位置和尺寸)
    view :rect参数是以view的左上角为坐标原点(0,0)
    arrowDirections :箭头的方向
    animated :是否通过动画显示出来
    

     rect和view参数

     设置显示的位置

    如果想让箭头指向某一个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];
    

     常见报错

    在popover的使用过程中,经常会遇到这个错误
    -[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区域外消失

    默认情况下
    只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的
    点击UIPopoverController区域外的控件,UIPopoverController默认会消失
    
    要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性
    @property (nonatomic, copy) NSArray *passthroughViews;
    这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了
    

     如何iPhone中实现popover的效果
    UIPopoverController这个类是只能用在iPad中的

    要想在iPhone中实现popover效果,必须得自定义view,可以参考
    http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000
    http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000

  • 相关阅读:
    我不是不懂
    Spring环境搭建,IoC容器初体验~
    备战招聘——信息获取与简历制作
    2013应届毕业生“华为”校招应聘总结
    Java知识积累——Sax解析xml文档
    Java知识积累——同时输出到控制台和指定文件,或直接输出到指定文件
    Java知识积累——静态代码块,非静态代码块,构造器的执行顺序和次数
    修改数据库表结构
    IE6/IE7/IE8/Firefox/Chrome/Safari的CSS hack兼容一览表
    项目代码风格要求
  • 原文地址:https://www.cnblogs.com/laugh/p/7120569.html
Copyright © 2020-2023  润新知