ios - 获取 UIButton 在 UITableViewCell 中的位置

标签 ios objective-c uitableview cgrect

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

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

For example: If the button is 8px away from the top of the current cell, but 57px away from the top of the viewController, I want to place the UIView at 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 的位置。

最佳答案

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

关于ios - 获取 UIButton 在 UITableViewCell 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145548/

相关文章:

ios - Swift:将 searchBar 固定为 tableView header

ios - SpriteKit : how to smoothly animate SKCameraNode while tracking node but only after node moves Y pixels?

ios - 不同 View Controller 中的同名变量

ios - 需要帮助将新的存储属性添加到 UITableView

iphone - objective-c : How to highlight the first cell in table view when view loads?

ios - 是否可以在其他 UI 元素后面显示 Popover?

ios - 我的数据格式有什么问题?

ios - 通过 Storyboard删除 UITableViewCell 中内容 View 的填充

iphone - @property(nonatomic)ivar @property(nonatomic,assign)ivar 相同还是不同?

objective-c - MacOSX 声音资源列表