Size of superview

Hi

1. I'm adding a UIView to a UIScrollView.
2. I set the frame for the UIView to CGRectMake(0, 0, 200, 200) and thus expect the view to be 200 in width and 200 in height.
3. I then add a child UIView to my UIView.
4. In the child view's view controller I then try to resize the child UIView to the same size as it's parent.

The problem is that it expands to the width and height of the entire application window, instead of just the parent view. Also, if I try to get the width and height of the parent UIView from the child view's view controller using self.view.superview.frame.size or self.view.superview.bounds.height I get the width and height of the entire application window instead of just the parent view.

Why is this? Shouldn't self.view.superview.bounds or self.view.superview.frame (what's the difference between these two properties, by the way?) return a width and height of 200, since that's what I set the frame of the superview to when I added it to my UIScrollView?

Message was edited by: Andreas Lindahl

Windows XP

Posted on Feb 26, 2009 5:21 AM

Reply
3 replies

Feb 26, 2009 10:18 AM in response to Andreas Lindahl

OK, here's some code that adds a bunch of views to a scroll view:

const int cellWidth = 160;
const int cellHeight = 126;
const int cellMargin = 1;
const int cols = 2;

NSInteger currentRow = 0;
NSInteger currentCol= 0;

int i = 0;

for (RoadSign *roadSign in self.roadSigns) {
currentCol = i % cols;
if (currentCol == 0 && i != 0)
currentRow++;

float xPos = (float)currentCol * (cellWidth + cellMargin);
float yPos = (float)currentRow * (cellHeight + cellMargin);

RoadSignViewController *roadSignView = [[RoadSignViewController alloc] init];
roadSignView.roadSign = roadSign;
roadSignView.parentViewController = self;
roadSignView.view.frame = CGRectMake(xPos, yPos, cellWidth, cellHeight);

[scrollView addSubview: roadSignView.view];

[roadSign release];
i++;
}

int contentSizeHeight = ((currentRow + 1) * (cellHeight + cellMargin)) - cellMargin;
scrollView.contentSize = CGSizeMake(320, contentSizeHeight);
self.scrollViewChild.frame = CGRectMake(0, 0, 320, contentSizeHeight);

I set the frame of each child view to different x,y coordinates but with the same width and height (cellWidth = 160, cellHeight = 126)

In my child view (RoadSignViewController) I then try to access the width and height of the view and assign the values to the width and height of a child view (backgroundView in the code below), but the child view expands to fill the entire scroll view, and not just it's superview (i.e. the size of the RoadSignViewController view.

- (void)viewDidLoad {
//Background view
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
[backgroundView release];
}

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Size of superview

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.