使用UIPopoverBackgroundView类

And*_*kov 10 ios ios5 uipopoverbackgroundview

Apple缺少有关如何使用UIPopoverBackgroundViewiOS5中引入的类的文档.有人有例子吗?

我试图将其子类化,但我的Lion上的XCode 4.2缺失了 UIPopoverBackgroundView.h

编辑:不出所料,它应该被导入为#import <UIKit/UIPopoverBackgroundView.h>

jrt*_*ton 13

要添加到另一个,仅链接的答案,这是如何完成的.

  • 创建UIPopoverBackgroundView的新子类
  • 在您的界面中声明以下内容:

    +(UIEdgeInsets)contentViewInsets;
    +(CGFloat)arrowHeight;
    +(CGFloat)arrowBase;
    
    @property(nonatomic,readwrite) CGFloat arrowOffset;
    @property(nonatomic,readwrite) UIPopoverArrowDirection arrowDirection;
    
    Run Code Online (Sandbox Code Playgroud)
  • 类方法很简单:contentViewInsets返回边框的宽度(不包括箭头),arrowHeight是箭头的高度,arrowBase是箭头的基础.

  • 实现两个属性设置器,确保调用[self setNeedsLayout].
  • 在初始化方法中,创建两个图像视图,一个包含箭头(应该是类方法中箭头尺寸的大小),另一个包含背景图像(必须是可调整大小的图像)并将其添加为子视图.在此处放置子视图无关紧要,因为您没有箭头方向或偏移.您应确保箭头图像视图位于背景图像视图上方,以便正确混合.
  • 实施layoutSubviews.在这里,根据arrowDirectionarrowOffset属性,您必须调整背景视图和箭头视图的框架.
    • 背景视图的框架应该在箭头所在的任何边缘上self.bounds插入arrowHeight
    • 箭头视图的框架应对齐,以使中心arrowOffset远离中心self(根据轴校正).如果箭头方向不对,你必须改变图像方向,但我的弹出窗口只会向上,所以我不这样做.

这是layoutSubviews我的Up-only子类的方法:

-(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];

    }
}
Run Code Online (Sandbox Code Playgroud)