像UIPopoverController中的方式一样关闭自定义UIView

时间:2012-04-30 14:54:43

标签: ios uiview uipopovercontroller

我正在建立一个类似于UIPopover视图的自定义UIView,只是我将UIView类子类化并在里面构建控件和事件的东西..来显示这个视图我通过我的子类数据源分配superView像这样

    if ([dataSource respondsToSelector:@selector(containerView)]) 
        superView = [dataSource containerView];

并显示它我有一个像这样做的功能

- (void) showPopOverFromRect : (CGRect) rect
{
    CGSize popSize = self.frame.size;

    float yPoint;

    if(ntPopOverDirection == NTPopOverArrowDirectionUP)
        yPoint = rect.origin.y + 10;
    else
        yPoint = rect.origin.y - 10;

    self.frame = CGRectMake(rect.origin.x - popSize.width, yPoint , popSize.width, popSize.height);

    [superView addSubview:self];
}

所以我的问题..如果用户点击superView上的AnyWhere就像UIPopOverController一样,我怎么能忽略这个视图(删除它)?

2 个答案:

答案 0 :(得分:2)

我建议您创建自定义UIView以使用清晰背景或径向渐变填充整个超视图或整个屏幕。然后你会在其中放置另一个具有popover外观的UIView。

这消除了尝试捕获点击并从其他视图发送通知的问题。它将全部自成一体。

您可以在自定义视图中轻松添加手势识别器,以便在用户触摸清除区域时关闭视图。

答案 1 :(得分:-1)

你可以在你的新UIView下方放置一个UIButton。按下此新按钮时,它会关闭您的视图并从superview中移除。

类似的东西:

- (void) showPopOverFromRect : (CGRect) rect
{
    CGSize popSize = self.frame.size;

    float yPoint;

    if(ntPopOverDirection == NTPopOverArrowDirectionUP)
        yPoint = rect.origin.y + 10;
    else
        yPoint = rect.origin.y - 10;

    self.frame = CGRectMake(rect.origin.x - popSize.width, yPoint , popSize.width, popSize.height);

    UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.backgroundColor = [UIColor clearColor];
    dismissButton.frame = [[UIScreen mainScreen] bounds];
    [dismissButton addTarget:self.delegate action:@selector(dismissPopover) forControlEvents:UIControlEventTouchUpInside];
    [superview addSubview:dismissButton];

    [superView addSubview:self];
}

您必须设置视图以将其超级视图设置为接收消息以取消弹出窗口的委托。