Ios 像在UIPopoverController中那样关闭自定义UIView
我正在构建一个类似于UIPopover视图的自定义UIView,只是我将UIView类划分为子类,并在其中构建控件和事件的内容。。为了显示这个视图,我通过我的子类数据源分配superView,如下所示Ios 像在UIPopoverController中那样关闭自定义UIView,ios,uiview,uipopovercontroller,Ios,Uiview,Uipopovercontroller,我正在构建一个类似于UIPopover视图的自定义UIView,只是我将UIView类划分为子类,并在其中构建控件和事件的内容。。为了显示这个视图,我通过我的子类数据源分配superView,如下所示 if ([dataSource respondsToSelector:@selector(containerView)]) superView = [dataSource containerView]; 为了证明这一点,我有一个这样的函数 - (void) showPo
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];
}
所以我的问题。。如果用户像UIPopOverController一样点击superView上的任意位置,我如何关闭此视图(将其删除) 您可以在新UIView下方放置一个清晰的UIView按钮。当按下此新按钮时,它将取消您的视图并从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];
}
但是,您必须设置视图,将其superview设置为接收消息的代理,以关闭popover。我建议您创建自定义UIView,以使用清晰的背景或径向渐变填充整个superview或整个屏幕。然后在这里面,你会放另一个UIView,它的外观和感觉与popover相似 这消除了试图捕获点击和从其他视图发送通知的问题。它将是完全独立的
您可以轻松地在自定义视图中添加一个手势识别器,以便在用户触摸清除区域时关闭视图。哦,我怎么会错过这个!经过一些修改,它就可以工作了。。谢谢。如果用户点击popover的左侧、右侧或上方该怎么办?他们不会点击“清除”按钮。@bbarnhart但在UIPopController中,如果你点击除popover之外的任何地方,并且在它的箭头旁边,它将被取消!那么这有什么错呢?在@Jacob comment中,我把“below”理解为自定义视图的Y坐标,而不是Z坐标。但是,在代码中,我看到按钮填充了整个屏幕。但是,使用UIButton而不是UIApgestureRecognitizer似乎很草率。@bbarnhart,有什么特别的原因吗?对我来说,这是UITapGestureRecognizers(iPhoneOS 2.0)出现前几天的遗物,但对于为什么它会比您的解决方案“更糟糕”来说,它并没有任何严重的影响。如果有任何问题,您可以为按钮上色以进行调试,以确保没有任何与点击坐标重叠或冲突的问题。此外,如果您正在放置UITapGestureRecognitor的superview没有填满整个屏幕,而是在导航控制器中,该怎么办?