views:

161

answers:

1

I am trying to figure out the best way to create a view like the app details screen in the AppStore app. I want a thumbnail and text content under it that all scrolls if the content is too long. Is this done in a tableview or a scrollview?

+3  A: 

I've made one in a scrollview. I calculated the size of each element's frame from this method:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

I kept a running total of the y size by adding it on after each label. At the end, if the scroll view was over a certain size (the length of my page) I gave that size to the scroll view, adding a little on the end so it wouldn't bump against the bottom.

Here's some code:

int currentYPos;
CGSize maximumSize = CGSizeMake(300, 9999);
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;

scrollView.pagingEnabled = NO;

// set the title frame size
self.titleLabel.text = self.title;

CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font 
      constrainedToSize:maximumSize 
    lineBreakMode:self.titleLabel.lineBreakMode];

currentYPos = titleSize.height + 20;

CGRect titleFrame = CGRectMake(10, 0, 300, currentYPos);
self.titleLabel.frame = titleFrame;

Note that many of the titleLabel properties were set on a label in IB.

nevan
So the view you built was done in IB but you used code to resize it on the fly?
SonnyBurnette
Yeah, but that was just because I started in IB, then discovered how to format the view later. You could set up your own labels in Xcode, the only difference is that you'll have to specify their font and line height etc. in the code.
nevan