如何以编程方式实现ScrollView

时间:2010-11-13 00:17:44

标签: xcode uiscrollview

我正在从UITableView启动详细信息页面,在该详细信息页面中有一个图像。我希望它是可缩放的,所以我希望该详细信息页面的内容位于scrollView中。到目前为止没有运气,标签,文本视图和图像都很好,但我无法实现ScrollView。

这是我的.m:谢谢!

- (void)viewDidLoad {

 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
 scrollView.contentSize = CGSizeMake(320, 700);
 scrollView.clipsToBounds = YES;
 super.view.backgroundColor = [UIColor whiteColor];
 [scrollView setUserInteractionEnabled:YES];




 self.navigationItem.title = [NSString stringWithFormat:selectedCellItem];



 CGRect frame = CGRectMake(0, 0, 10, 10);

 self.view = [[UIView alloc] initWithFrame:frame];

 self.view.backgroundColor = [UIColor whiteColor];



 frame = CGRectMake(0, 320, 10, 10);



 UILabel *label = [[UILabel alloc] initWithFrame:frame];

 //label.text = selectedCellItem;


 label.font=[UIFont fontWithName:@"Verdana-Bold" size:20.0];
 label.textAlignment = UITextAlignmentCenter;

 [self.view addSubview:label];

 [label release];

 frame = CGRectMake(0, 320, 320, 60); //Description



 UITextView *labelDesc = [[UITextView alloc] initWithFrame:frame];

 labelDesc.text = selectedCellDesc;
 labelDesc.font=[UIFont fontWithName:@"Verdana" size:12.0];
 labelDesc.textAlignment = UITextAlignmentLeft;

 //labelDesc.textAlignment = UITextAlignmentTop;

 [self.view addSubview:labelDesc];

 [labelDesc release];




 frame = CGRectMake(0, 0, 320, 320); //Image

 UIImageView *imageItem = [[UIImageView alloc] initWithFrame:frame];
 [imageItem setImage:[UIImage imageNamed:selectedCellImage]];

 // GET THIS ONE WORKING RIGHT>>[imageItem setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [[NSBundle mainBundle] bundlePath], selectedCellImage]]]; //use 

 //imageItem = [[UIImageView alloc] initWithContentsOfFile:[NSString stringWithFormat:@"shirt.png", [[NSBundle mainBundle] bundlePath]]];
 //labelDesc.font=[UIFont fontWithName:@"Zapfino" size:13.0];
 //labelDesc.textAlignment = UITextAlignmentCenter;

 imageItem.contentMode = UIViewContentModeScaleAspectFit;
 [self.view addSubview:imageItem];






 //[imageItem release];

    [super viewDidLoad];

}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

1 个答案:

答案 0 :(得分:0)

我认为您缺少以下行来将UIScrollView添加为子视图。

[self.view addSubView:scrollView];