ios - 如何在 UITable 中获取 UIButton 或 UILabel 的位置?

标签 ios uitableview

我在所有 UICellViews 中都有按钮.按下这些按钮时,会触发一些操作。到目前为止,一切都很好。

如何获取这些按钮之一相对于屏幕上可见区域的位置?您对如何实现这一目标有任何想法(或教程)吗?

最佳答案

您可以使用方法 -convertRect:toView: UIView .

CGRect frameInWindow = [button convertRect:button.bounds toView:viewController.view];

这是一个小测试:
- (void)loadView
{
    [super loadView];

    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

    UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
    [self.view addSubview:parentView];

    UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [parentView addSubview:childView];

    CGRect rect1 = [childView convertRect:childView.bounds toView:self.view];
    // 200, 200, 100, 100 => correct

    // Other tests for the discussion of @H2CO3's answer.

    CGRect rect2 = [childView.superview convertRect:childView.frame toView:self.view];
    // 200, 200, 100, 100 => correct
    CGRect rect3 = [childView convertRect:childView.frame toView:self.view];
    // 300, 300, 100, 100 => wrong
}

关于ios - 如何在 UITable 中获取 UIButton 或 UILabel 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238411/

相关文章:

ios - 在 UIButton/UITableViewCell/UICollectionViewCell 选择上禁用 VoiceOver

ios - 无法将自定义字体添加到 Xcode

iphone - 单击按钮时根据字母或数字顺序对 UITableView 进行排序

ios - 在 tableView : willDisplayHeaderView: forSection: 的 UITableViewHeaderFooterView 中垂直居中 textLabel

ios - 在 Firebase 上查询多个键

ios - 如何在 SpriteKit 中只激活物理体的一侧?

ios - 如何在 iOS 中编辑来自另一个应用程序的文本

ios - 从服务器下载@2x 和@3x 图像到 Tableview

objective-c - 插入没有任何动画的 UITableView 行

ios - PFQueryTableViewController 错误