Prashant Solanki
Prashant Solanki

Reputation: 112

how to resize UIScrollview dynamically

well i working with to zoom image with UIScrollview..my UIScrollview size is fix so whenever i make a zoom of a image then it goes cut..i wanna do that as i zoom the image the UIScrollview also increse height and width as per image zoom.. any suggetions will be appreciated......

Upvotes: 2

Views: 6877

Answers (1)

simonbs
simonbs

Reputation: 8042

This should work :-)

  1. Make a UIImage
  2. Make a UIImageView holding the image
  3. Put the UIImageView into a UIScrollView
  4. Set the contentSize of the UIScrollView to the size of the UIImage
  5. Remember to return the UIimageView in viewForZoomingInScrollView

Put this into your .h file:

UIImageView *imageView;

@property (nonatomic, retain) UIImageView *imageView;

And remember to synthesize and release imageView

Put this where you want to make your scroll view:

UIImage *image = [UIImage imageNamed:@"myImage.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:3.0];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
[scrollView release];

Add the UIScrollView delegate method

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

UPDATE:

I misunderstood the question when adding the above solution. This should do it :-)

kScrollViewAddSize is a constant which defines the amount to add to the UIScrollView's size.

In your .h file you should add the following:

UIImageView *imageView;
UIScrollView *scrollView;

Remember to synthesize and release them.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"apple.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(kScrollViewAddSize / 2, kScrollViewAddSize / 2, image.size.width, image.size.height)];
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, width, height)];
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:3.0];
    [scrollView setBackgroundColor:[UIColor redColor]];
    [scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
    [scrollView setDelegate:self];
    [scrollView addSubview:imageView];
    [[self view] addSubview:scrollView];
}

- (void)scrollViewDidZoom:(UIScrollView *)_scrollView {
    int width = imageView.frame.size.width + kScrollViewAddSize;
    int height = imageView.frame.size.height + kScrollViewAddSize;
    [scrollView setFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, imageView.frame.size.width + kScrollViewAddSize, imageView.frame.size.height + kScrollViewAddSize)];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)_scrollView {
    return imageView;
}

Add the parth in viewDidLoad where you want to create your UIScrollView and add the two delegate methods.

Upvotes: 3

Related Questions