I should know this but don't and can't find it explained anywhere.
I am moving a UIView
in the coordinate space of the window and would like its subview (a tableView) added in code to move as well. I have not added any explicit constraints linking the subview to its superview thinking they would move in tandem. The tableview is not moving, however, as far as I can tell, when I move the superview.
Is it normal behavior for a subview created in code to be unaffected by changing the coordinates of its superview? If so, do you have to add constraints in code, should you manually move the subviews at the same time you are moving the superview or how can you get the subview to move in tandem? Here is code:
//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 moves but Tableview does not seem to move from this alone. How can I move it?
I'm assuming you say "myView moves but Tableview does not seem to move" because you don't see Tableview on-screen? If so, it looks like it's due to the way you set the frame(s).
//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];
OK - myView is now off-screen to the left. Let's assume a width of 320 and a height of 480, so your myView's frame is (for example):
`-320, 0, 320, 480`
then
aTableView = [UITableView new];
//Initially set frame to superview
aTableView.frame = myView.frame;
[myView addSubview:aTableView];
Whoops, you set aTableView.frame = myView.frame;
That means your table's frame is:
`-320, 0, 320, 480`
but that is relative to myView's frame. So your table view is located 320-pts
to the left of myView, which comes out to 640-pts
to the left of the left edge of the screen.
//Move superview on screen
myRect = CGRectMake(0,0,width,height)];
myView.frame = myRect;
Now you've moved myView's left to 0
, so it's visible, but aTableView
is still 320-pts
to the left of myView
, so it's still off-screen.
Changing that one line to:
aTableView.frame = myView.bounds
should take care of it.