试图复制UIView的框架最终会产生负偏移

时间:2013-09-09 13:53:48

标签: ios objective-c uiview interface-builder

我正在摆弄Interface Builder并得到这个令人沮丧的补偿问题:

我创建了一个带有一些子视图层次结构的视图:

[parent view]
    |--> (dummy backround)
      |--> (a template UIView)

模板就在那里,因此设计师应该能够调整计划的更复杂视图的坐标。我想将坐标复制到以编程方式创建的视图中。这就是我的工作:

 UIView *theView = [[UIView alloc] initWithFrame:pagesBounds.frame];
 theView.backgroundColor = [UIColor grayColor];
 [self.view addSubview:theView]; // self.view is the parent view from above

到目前为止一切顺利,但我看到theView中的负偏移可以在左下角最佳显示:

enter image description here

我的目标是以编程方式创建的视图(较暗的灰色视图)应将其自身定位在模板视图(白色视图)的正上方。我已经浏览了IB中的视图并确保所有视图的“自动调整子视图”都已关闭(尽管实际视图已添加到最顶层)。

显然我仍然缺少一些东西。建议?

1 个答案:

答案 0 :(得分:2)

发生偏移的原因是因为当你得到“模板UIView”的帧时,它是根据“虚拟背景”的坐标来描述的。要正确定位,你应该做这样的事情:

UIView *theView = [[UIView alloc] initWithFrame:CGRectOffset(pagesBounds.frame, dummyBackground.frame.origin.x, dummyBackground.frame.origin.y)];
theView.backgroundColor = [UIColor grayColor];
[self.view addSubview:theView]; // self.view is the parent view from above