Reputation:
I'm trying to make an app for a restaurant and the to display the food menu as a scrollable image. The image is pulled form a config file.
- (void)viewDidLoad
{
[super viewDidLoad];
// back button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"btn-back.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 46.0f, 28.0f);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
[self setTitle:[menu objectForKey:@"title"]];
[scrollView2 setScrollEnabled:YES];
NSString *imageName = [menu objectForKey:@"imageName"];
if (![imageName isEqualToString:@""]) {
UIImage *image = [UIImage imageNamed:imageName];
[imageView setImage:image];
[scrollView2 addSubview:imageView];
[imageView release];
}
[textView setText:[menu objectForKey:@"description"]];
}
As of now the image doesn't scroll, but appears as a static image. Could anyone please help me modify this code so the user can vertically scroll the image. Thanks a lot.
Upvotes: 2
Views: 6701
Reputation: 76
I agree mostly with what Seega wrote. Your imageView must be subview of a scrollView. Then, don't forget to set the scrollView's contentSize & the minimum&maximumZoomScale & and the imageView's frame:
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.minimumZoomScale = // some number between 0 and 1, default is 1, so no zoom
self.scrollView.maximumZoomScale = // some number larger than 1, default is 1, so no zoom
self.scrollView.zoomScale = 1 // starting zoom scale
A nice place to put this code is in
-(void)viewWillLayoutSubviews{}
Thus it will be executed everytime the view appears & also whenever the device is rotated.
Upvotes: 0
Reputation: 3400
You should use a UIScrollView
and add the UIImageView
as subview of the contentView.
And then set the contentsize of the scrollview as the imageviews size.
[imageView setImage:image];
[scrollView2.contentView addSubview:imageView];
[scrollView2 setContentSize:image.frame.size];
[imageView release];
have no Mac to test at the moment but that should work.
Upvotes: 2