I want to achieve a UIScrollview, that contains an UIImageView that is centered and zooms without flickering, with only using Layout-Constraints.
I've created a UIScrollView and the UIImageView add this as subviews to a normal UIView.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.scrollView.maximumZoomScale = 10.0f;
self.scrollView.delegate = self;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:self.imageView];
[self.view addSubview:self.scrollView];
Than I add the Constraints for these Views:
NSDictionary *viewsDictionary = @{@"scrollView" : self.scrollView,
@"imageView" : self.imageView};
CGRect imageFrame = CGRectMake(0, 0, 184, 111);
NSDictionary *metrics = @{@"width" : @(self.view.frame.size.width),
@"height" : @(self.view.frame.size.height),
@"imageWidth" : @(widgetFrame.size.width),
@"imageHeight" : @(widgetFrame.size.height)};
NSArray *scrollViewVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(height)]" options:0 metrics:metrics views:viewsDictionary];
NSArray *scrollViewHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView(width)]" options:0 metrics:metrics views:viewsDictionary];
NSArray *imageViewVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageView(imageHeight)]" options:0 metrics:metrics views:viewsDictionary];
NSArray *imageViewHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView(imageWidth)]" options:0 metrics:metrics views:viewsDictionary];
NSLayoutConstraint *imageCenterXConstraint = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *imageCenterYConstraint = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
[self.view addConstraints:scrollViewVerticalConstraints];
[self.view addConstraints:scrollViewHorizontalConstraints];
[self.view addConstraints:imageViewVerticalConstraints];
[self.view addConstraints:imageViewHorizontalConstraints];
[self.view addConstraints:@[imageCenterXConstraint, imageCenterYConstraint]];
Now it looks like it should... a centered UIImageView within a UIScrollView that can be zoomed (through the UIScrollViewDelegate method).
But I have a problem when I finished the Zoom-Gesture the UIImageView shortly move to a other position and returns immediately to the center. By zooming in it moves in the direction to the top left corner, by zooming out in the direction of the bottom right corner.
Why the hell it do that?
Can somebody give me a hint? I looked through SO but found no answer in relation to a centered UIImageView.
MWZoomingScrollView
file. It does exactly that. You can easily make out how to do it, just by studying the code. Hint : It depends onlayoutSubviews
. – n00bProgrammer