I have a UIScrollView, inside which I have some UILabels and a UITextView. The UIScrollView is a subview of a master UIView. The UIScrollView is initialized as:
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 75, rct.size.width-50, 1000)];
The UILabels are added to it as subviews:
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor greenColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16]];
lbl.text = [NSString stringWithFormat:@"name: %@",firstName];
[scrollView addSubview:lbl];
[lbl release];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 40)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor redColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16]];
lbl.text = [NSString stringWithFormat:@"last name: %@",lastName];
[scrollView addSubview:lbl];
[lbl release];
.
.
// TEXT VIEW
UITextView *txt1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 160, rct.size.width-50, 1000)];
txt1.textAlignment = UITextAlignmentLeft;
txt1.textColor = [UIColor whiteColor];
[txt1 setBackgroundColor:[UIColor clearColor]];
UIFont *f = [UIFont fontWithName:@"Arial-BoldMT" size:16];
[txt1 setFont:f];
txt1.text = [NSString stringWithFormat:@"info: %@",personInfo];
[scrollView addSubview:txt1];
CGRect newframe1 = txt1.frame;
newframe1.size.height = txt1.contentSize.height + 3; // offset for shadow
txt1.frame = newframe1;
[txt1 release];
The TextView is added as a hack (shown here), and is basically the only one scrollable, the other views remain static, I need them to scroll as well.
EDIT: When I add a UILabel even outside the bounds of the scrollView it won't scroll there:
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 940, 100, 1000)];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:[UIColor whiteColor]];
[lbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
lbl.text = [NSString stringWithFormat:@"TEST"];
[scrollView addSubview:lbl];
[lbl release];
The scrollView won't budge!
contentSize
. Also, you should consider using ARC (Automatic Reference Counting), as this will make your app more reliable. – Andreas Ley