4

I'm having a lot of trouble doing something very simple. I have a viewController and a subViewController, both with views loaded from nibs. For clarification the parent view is sized 1024 by 748 and the subView is 640 by 480 in the nibs.

On the viewController's viewDidLoad I add the subViewController's view like so:

[self.view addSubview:self.subViewController.view];
NSLog(@"subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

The log outputs:

subview.frame:{{0, 0}, {640, 480}}

Then, just to test things out, I use setCenter to attempt to reposition the subview within the parent view at the same position like so:

CGPoint newCenter = CGPointMake(0,0);  //should stay at the same position..
[self.subViewController.view setCenter:newCenter];
NSLog(@"after changing center.subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

The log outputs:

after changing center. subview.frame:{{-320, -240}, {640, 480}}

Now I have a feeling that something might be getting screwed up in the nibs and am guessing it's something to do with the springs & struts / size & position options. I can only guess it's this because this is the feature I have least understanding of when it comes to IB.

Any other ideas as to what could be causing this issue? Do you guys have any tips in general on how to set up viewcontrollers' views in IB so that they are automatically positioned in alignment with other viewcontrollers' views? And after setting things up, is setCenter the correct way of moving them around dynamically?

donalbain
  • 1,158
  • 15
  • 31

2 Answers2

6

The outputs are correct given what you are doing. The point (0,0) is the top-left corner. If you set the center of subview to this, then the top left corner of the subview will indeed be (-640/2, -480/2).

To get the top-left corners to align, use

[self.subViewController.view setCenter:CGPointMake(320.0,240.0)];
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • Ok I see. I think my misunderstanding was that I thought the origin of the frame was the same as the center as opposed to the top left corner. Stupid mistake. Is there a way to set the initial center of a view in IB? – donalbain Mar 18 '11 at 19:21
  • Sorry for the noobiness. I honest to god never noticed the check button below the rating arrows x_O – donalbain Mar 18 '11 at 19:31
  • No worries there. In the IB, the third tab (with a ruler) in the Attributes window will indicate the position and size of any subviews. This allows you to align the center, sides or corners precisely without doing it programmatically. – PengOne Mar 18 '11 at 20:34
0

The problem is unclear. If you set the center to 0,0, then you should expect the frame origin to be -320, -240 as it appears to be. Perhaps you misunderstand CGRect? CGRect is made up of origin and size. Frame is a CGRect. Frame, size and center are all related, and changing frame changes size and center, and changing center changes frame.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610