苹果公司缺乏有关如何使用iOS5中引入的UIPopoverBackgroundView
类的文档。是否有人有示例?
我尝试过子类化它,但我的Lion上的XCode 4.2缺少UIPopoverBackgroundView.h
文件。
编辑:不出所料,应该将其导入为#import <UIKit/UIPopoverBackgroundView.h>
苹果公司缺乏有关如何使用iOS5中引入的UIPopoverBackgroundView
类的文档。是否有人有示例?
我尝试过子类化它,但我的Lion上的XCode 4.2缺少UIPopoverBackgroundView.h
文件。
编辑:不出所料,应该将其导入为#import <UIKit/UIPopoverBackgroundView.h>
创建一个UIPopoverBackgroundView的新子类。
在您的接口中声明以下内容:
+(UIEdgeInsets)contentViewInsets;
+(CGFloat)arrowHeight;
+(CGFloat)arrowBase;
@property(nonatomic,readwrite) CGFloat arrowOffset;
@property(nonatomic,readwrite) UIPopoverArrowDirection arrowDirection;
这个类的方法很简单: contentViewInsets
返回你所有边框的宽度(不包括箭头), arrowHeight
是你的箭头高度, arrowBase
是你的箭头底部。
[self setNeedsLayout]
。layoutSubviews
。在这里,根据arrowDirection
和arrowOffset
属性,你必须调整背景视图和箭头视图的框架。
self.bounds
,在箭头所在的任何边缘上缩进arrowHeight
self
的中心点相隔arrowOffset
(根据坐标轴正确对齐)。如果箭头方向不是向上,则必须更改图像方向,但我的气泡窗口只能向上,所以我没有这样做。这是我只向上子类的layoutSubviews
方法:
-(void)layoutSubviews
{
if (self.arrowDirection == UIPopoverArrowDirectionUp)
{
CGFloat height = [[self class] arrowHeight];
CGFloat base = [[self class] arrowBase];
self.background.frame = CGRectMake(0, height, self.frame.size.width, self.frame.size.height - height);
self.arrow.frame = CGRectMake(self.frame.size.width * 0.5 + self.arrowOffset - base * 0.5, 1.0, base, height);
[self bringSubviewToFront:self.arrow];
}
}
UIPopoverBackgroundView
比你想象的更费力,因为文档有限。这个 Github 项目有一个完整的工作示例,为我节省了很多时间: https://github.com/GiK/GIKPopoverBackgroundView
将它集成到你自己的项目中相当简单。最棘手的部分是根据你使用的自定义图片调整 cap insets。我建议在项目中原地进行自定义,并且很容易在模拟器中验证所有弹出窗口方向和方位的用例是否正确显示,然后再将其迁移到你自己的项目中。