我在所有 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/