【问题标题】:Dismiss a custom UIView like the way in UIPopoverController像 UIPopoverController 中的方式一样关闭自定义 UIView
【发布时间】:2012-04-30 14:54:43
【问题描述】:

我正在构建一个类似于 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];
}

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

【问题讨论】:

    标签: ios uiview uipopovercontroller


    【解决方案1】:

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

    这消除了尝试捕获点击和从其他视图发送通知的问题。它将是自包含的。

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

    【讨论】:

      【解决方案2】:

      您可以在新的 UIView 下方放置一个清晰的 UIButton。当按下这个新按钮时,它会关闭您的视图并将自己从超级视图中删除。

      类似:

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

      但是,您必须设置视图以将其超级视图设置为接收消息以关闭弹出框的委托。

      【讨论】:

      • 哦,我怎么错过了!经过一些修改它可以工作..谢谢。
      • 如果用户点击弹出框的左侧或右侧或上方会怎样?他们不会点击“清除”按钮。
      • @bbarnhart 但在 UIPopController 中,如果您点击弹出框以外的任何位置,并且在其箭头旁边,它将被关闭!那有什么问题呢?
      • 在@Jacob 评论中,我将“下方”表示在自定义视图的 Y 坐标中,而不是 Z 坐标中。但是,在代码中,我看到按钮填满了整个屏幕。但是,使用 UIButton 而不是 UITapGestureRecognizer 似乎很草率。
      • @bbarnhart,有什么特别的原因吗?对我来说,这是 UITapGestureRecognizers (iPhoneOS 2.0) 之前的遗留物,但对于为什么它会比你的解决方案“更糟”并没有任何严重的后果。如果有的话,您可以为按钮着色以进行调试,以确保点击坐标重叠或冲突没有任何问题。另外,如果您放置 UITapGestureRecognizer 的超级视图没有填满整个屏幕而是在导航控制器中怎么办?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      相关资源
      最近更新 更多