使用UIPopoverBackgroundView

使用UIPopoverBackgroundView

本文介绍了使用UIPopoverBackgroundView类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple缺少有关如何使用iOS5中引入的 UIPopoverBackgroundView 类的文档。有人有一个例子吗?

Apple is missing documentation on how to use UIPopoverBackgroundView class introduced in iOS5. Anyone have an example?

我试图将它子类化,但我的Lion上的XCode 4.2缺少 UIPopoverBackgroundView.h

I have tried to subclass it, but my XCode 4.2 on Lion is missing UIPopoverBackgroundView.h

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

推荐答案

要添加到另一个,仅链接答案,这里是如何做到的。

To add to the other, link-only answers, here is how this is done.


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

  • Create a new subclass of UIPopoverBackgroundView
  • Declare the following in your interface:

+(UIEdgeInsets)contentViewInsets;
+(CGFloat)arrowHeight;
+(CGFloat)arrowBase;

@property(nonatomic,readwrite) CGFloat arrowOffset;
@property(nonatomic,readwrite) UIPopoverArrowDirection arrowDirection;


  • 类方法很简单: contentViewInsets 一直返回边框的宽度(不包括箭头), arrowHeight 是箭头的高度, arrowBase 是你箭头的基础。

  • The class methods are straightforward: contentViewInsets returns the width of your borders all the way round (not including the arrow), arrowHeight is the height of your arrow, arrowBase is the base of your arrow.


    • 背景视图的框架应为 self.bounds ,由 arrowHeight 在箭头所在的任何边缘

    • 箭头视图的框架应对齐,以便中心 arrowOffset 远离 self 的中心(根据轴校正)。如果箭头方向不正常,你必须改变图像方向,但我的弹出窗口只会向上,所以我没有这样做。

    • The frame of your background view should be self.bounds, inset by arrowHeight on whatever edge the arrow is on
    • The frame of the arrow view should be aligned so that the centre is arrowOffset away from the centre of self (correct according to the axis). You have to change the image orientation if the arrow direction is not up, but my popover would only be up so I didn't do that.

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

    Here is the layoutSubviews method for my Up-only subclass:

    -(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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-13 19:17