問題描述
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 子類
在您的界面中聲明以下內容:
- 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
插入 - 箭頭視圖的框架應對齊,使中心遠離
self
中心arrowOffset
(根據軸正確).如果箭頭方向不向上,您必須更改圖像方向,但我的彈出框只會向上,所以我沒有這樣做.
- The frame of your background view should be
self.bounds
, inset byarrowHeight
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 ofself
(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 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!