【问题标题】:Get position of UIButton in UITableViewCell在 UITableViewCell 中获取 UIButton 的位置
【发布时间】:2015-01-26 06:23:37
【问题描述】:

我在静态 UITableViewCell 中有一个按钮。我想要做的是在按钮位置放置一个 UIView。

如果我只是得到按钮的位置,它会给我一个相对于 tableviews 单元格的位置,而不是从 tableViewController 的顶部。我想要的是从 tableViewController 的顶部获取按钮位置。

例如:如果按钮距离当前顶部 8px 单元格,但距离 viewController 的顶部 57px,我想 将 UIView 放置在 57px。

这是我的代码:

- (void)viewDidLoad {
    CGPoint buttonPosition = [button convertPoint:CGPointZero fromView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

    CGRect rect = [myTableView rectForRowAtIndexPath:indexPath];

    NSLog(@"%f, %f", rect.origin.y, rect.origin.x);

    self.infoView = [[UIView alloc] initWithFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, 220, 110)];
}

nslog 的输出为 0。

我还想在 iphone 进入横向模式时更新 UIView 的位置。

【问题讨论】:

  • 这是自定义单元格?
  • 你有按钮实例吗?
  • 按钮从按钮数组中调用,如下所示: UIButton *button = [self.allButtons objecAtIndex:1];
  • 在您的 Y 位置添加将行前的行数乘以按钮和行高的结果。但我怀疑有更好的方法来做你想做的任何事情。那么告诉我们,你的真正目标是什么?
  • button.frame.origin.xbutton.frame.origin.y 为您提供按钮位置

标签: ios objective-c uitableview cgrect


【解决方案1】:
CGPoint originalPoint = button.frame.origin;
CALayer *layer = self;
CGPoint point = originalPoint;
while (layer.superlayer)
{
    point = [layer convertPoint:point toLayer:layer.superlayer];
    layer = layer.superlayer;
}

//When you reach this code, you should have the position in the rootView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2013-04-20
    • 2011-11-28
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    相关资源
    最近更新 更多