UIPopoverController初探

来源:互联网 发布:知悉和悉知是什么意思 编辑:程序博客网 时间:2024/06/07 00:58

最近开始学习iPad开发,接触到了许多新知识。与iPhone上的学习一样,我还是从Apple的官方文档开始看,遇到的第一个比较有代表性的就是UIPopoverController了。今天就来总结一下它的相关要点吧。(下面的引用内容均出自Apple官方文档)

The UIPopoverController class is used to manage the presentation of content in a popover. You use popovers to present information temporarily. The popover content is layered on top of your existing content and the background is dimmed automatically. The popover remains visible until the user taps outside of the popover window or you explicitly dismiss it. Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception。

UIPopoverController这个类是用来管理一个popover内容的展示的。你可以用它们来展示一些临时性的内容。在视图层次上,popover是处于已经存在视图的上方的,并且背景会自动模糊。除非用户点击了popover之外的节目或者直接关闭它,它都是会一直呈现在界面上的。Popover控制器是专门为iPad而生的,为iPad之外的设备创建它是毫无疑问错误的~

To display a popover, create an instance of this class and present it using one of the appropriate methods. When initializing an instance of this class, you must specify the view controller that provides the content for the popover. Popovers normally derive their size from the view controller they present. However, you can change the size of the popover by modifying the value in the popoverContentSize property or by calling the setPopoverContentSize:animated: method. The latter approach is particularly effective if you need to animate changes to the popover’s size. The size you specify is just the preferred size for the popover’s view. The actual size may be altered to ensure that the popover fits on the screen and does not collide with the keyboard

为了在界面上显示出一个popover,你需要一个创建这个类的实例然后调用合适的 方法来将其展示到节目上。当实例化这个类的时候,你必须指明为PopoverController提供内容视图的控制器。通常Popovers的大小遵循它们所呈现的内容视图的大小。然而,你可以通过改变popOverContentSize属性或者调用setPopoverContentSize:animated:方法来改变popover的大小。如果你需要让popvoer大小改变为动画效果的话,后一个方法更加适合。你所确定的大小仅仅是popover的preferred大小。实际的大小会依据适合屏幕和与键盘不发生冲突而改变。

When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.

当popvoer显示到屏幕上时,点击其之外的区域会自动关闭popover视图。为了允许用户与特定的视图进行交互而不关闭popover视图,你可以将一个或多个视图对象放入passthroughViews属性里。点击popover窗口内部是不会自动关闭ta它的,你的视图和视图控制器代码必须明确的处理popover里面动作和事件,调用dismissPopoverAnimated:来关闭popover.

If the user rotates the device while a popover is visible, the popover controller hides the popover and then shows it again at the end of the rotation. The popover controller attempts to position the popover appropriately for you but you can also implement the popoverController:willRepositionPopoverToRect:inView: method in the popover delegate to specify a new position.

如果当一个popover显示出来的时候,用户旋转设备,popoverController会隐藏掉popver然后在旋转完成后再把它显示出来。popoverController会自动帮你去设置popover合适的位置,但是你也可以通过实现popover的代理方法popoverController:willRepositionPopoverToRect:inView: 来指定一个新的位置。

    以上就是Apple官方文档里对PopoverController的简单介绍。通过上面的文字,我们不难理解PopoverController的展示方式与特性。那么,让我们通过代码实验一下吧。

1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:[[TableViewController alloc] init]];    [pop presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];    self.pop = pop;}

上面的代码在控制器的touchesBegan方法里添加了初始化popover的代码,那么就可以实现点击屏幕任何地方调出popover的效果了。这里,我们使用了一个tableViewController来作为popover的contentViewController。然后这里有个重要的方法,


/* Like the above, but is a convenience for presentation from a `UIBarButtonItem` instance. arrowDirection limited to UIPopoverArrowDirectionUp/Down

 */

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

这个方法需要传三个参数,分别是一个barButtonItem,一个枚举类型来指明popover小箭头的方向和一个布尔值来指明是否要呈现动画效果。

在模拟器中的效果如下:


除了上面的方法之外,还有一个方法来设置popover的位置。


在模拟器上效果如下


这里还有一个重要的知识点要指出,popover的contentViewController的大小最好不要在popover那里设置,而是交给cotentViewController自己去设置


除了preferredContentSize这个属性,还有一个属性contentSizeForViewInPopover,不过这个属性在iOS7就被废弃了。

通过设置passthroughViews属性来使其他控件在popover出现的时候仍能够与用户交互

0 0
原创粉丝点击