我应该知道这一点,但是在任何地方都找不到,也找不到。
我正在窗口的坐标空间中移动UIView
,并希望在代码中添加其子视图(一个tableView)也可以移动。我还没有添加任何明确的约束将子视图与其父视图链接起来,以为它们会串联移动。据我所知,当我移动超级视图时,tableview并没有动。
通过代码创建的子视图通过更改其父视图的坐标不受影响是正常行为吗?如果是这样,您是否必须在代码中添加约束,是否应该在移动父视图的同时手动移动子视图,或者如何使子视图串联移动?这是代码:
//Create view and subview (tableView):
myView= [UIView new];
CGFloat width = self.view.frame.size.width;
CGFloat height=self.tableView.frame.size.height;
//Place offscreen
[myView setFrame:CGRectMake(-width, 0, width, height)];
[self.view addSubview:myView];
aTableView = [UITableView new];
//Initially set frame to superview
aTableView.frame = myView.frame;
[myView addSubview:aTableView];
//Move superview on screen
myRect = CGRectMake(0,0,width,height)];
myView.frame = myRect;
myView移动了,但是Tableview似乎并没有因此而改变。我该如何移动?
答案 0 :(得分:1)
我假设您说“ myView移动了,但Tableview似乎没有移动” ,因为您没有在屏幕上看到Tableview?如果是这样,则似乎是由于您设置框架的方式所致。
//Create view and subview (tableView):
myView= [UIView new];
CGFloat width = self.view.frame.size.width;
CGFloat height=self.tableView.frame.size.height;
//Place offscreen
[myView setFrame:CGRectMake(-width, 0, width, height)];
[self.view addSubview:myView];
确定-myView现在不在屏幕左侧。假设宽度为320高度为480,因此myView的框架为(例如):
`-320, 0, 320, 480`
然后
aTableView = [UITableView new];
//Initially set frame to superview
aTableView.frame = myView.frame;
[myView addSubview:aTableView];
糟糕,您设置了aTableView.frame = myView.frame;
,这意味着表格的框架为:
`-320, 0, 320, 480`
但是 ,它是相对于myView框架的。因此,您的表格视图位于myView的左侧320-ptd
,位于屏幕左侧的左侧640-pts
。
//Move superview on screen
myRect = CGRectMake(0,0,width,height)];
myView.frame = myRect;
现在,您已经将myView的左侧移至0
,因此可见,但是aTableView
仍位于320-pts
的左侧,myView
,因此仍然不在屏幕上
将该行更改为:
aTableView.frame = myView.bounds
应该照顾它。