0
votes

I am trying to implement a simple ImageView in a scrollview to display an image. I want to particularly implement the pinch to zoom feature. I implemented the following code (which is taken from one of the Objective C sample codes on the apple websites). I tested the zooming thoroughly on ios 5 and there seems to be no problem. However when I ported my code to ios 6 and I test my zoom feature, after a while I get the following error message and my app crashes with skewed zoom results.

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

NSAutoresizingMaskLayoutConstraint:0x8808360 h=--- v=--- V:[UIWindow:0x744db00(480)], NSLayoutConstraint:0x71286d0 UIView:0x71222c0.bottom == UIWindow:0x744db00.bottom, NSLayoutConstraint:0x7128650 V:|-(20)-[UIView:0x71222c0] (Names: '|':UIWindow:0x744db00 ), NSLayoutConstraint:0x7120c50 UIImageView:0x7451630.bottom == UIScrollView:0x71206f0.bottom, NSLayoutConstraint:0x7120c90 V:|-(0)-[UIImageView:0x7451630] (Names: '|':UIScrollView:0x71206f0 ), NSLayoutConstraint:0x7120bd0 UIImageView:0x7451630.centerY == UIScrollView:0x71206f0.centerY, NSLayoutConstraint:0x7122a50 V:|-(10)-[UIScrollView:0x71206f0] (Names: '|':UIView:0x71222c0 ), NSLayoutConstraint:0x7122940 V:[UIScrollView:0x71206f0]-(38)-| (Names: '|':UIView:0x71222c0 )

Will attempt to recover by breaking constraint

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

I tried doing a google search for this problem but I am not getting good results. The code I am using is as follows. Help is greatly Appreciated

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // set the tag for the image view
    [self.imageView setTag:ZOOM_VIEW_TAG];

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    NSLog(@"[imageScrollView frame].size.width : %f",[self.scrollVIew frame].size.width);
    NSLog(@"[imageView frame].size.width : %f",[self.imageView frame].size.width);

    NSLog(@"[imageScrollView frame].size.height : %f",[self.scrollVIew frame].size.height);
    NSLog(@"[imageView frame].size.height : %f",[self.imageView frame].size.height);

    float minimumScale = [self.scrollVIew frame].size.width  / [self.imageView frame].size.width;
    minimumScale = 300.0/1200.0;

    NSLog(@"minimumScale : %f",minimumScale);

    [self.scrollVIew setMinimumZoomScale:minimumScale];
    [self.scrollVIew setZoomScale:minimumScale];

    NSLog(@"imageScrollView.zoomScale : %f",self.scrollVIew.zoomScale);
    NSLog(@"imageScrollView.minimumZoomScale = minimumScale : %f",self.scrollVIew.minimumZoomScale = minimumScale);

    self.scrollVIew.contentSize = CGSizeMake(1200.0, 800.0);
    self.scrollVIew.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [self.scrollVIew viewWithTag:ZOOM_VIEW_TAG];
}

Vivek

2

2 Answers

1
votes

You need to create your UIImageView Programatically, and add in a subview of your UIScrollView. But you must make this inside of the viewDidAppear.

Just your UIScrollView can be made in StoryBoard!

And you will set the scrollView.contentSize inside of "viewDidAppear:(BOOL)animated" and not inside of the viewDidLoad.

0
votes

Disable auto layout in your XIB file. It will be under the left most tab in the properties section on the right. You are probably using it without knowing it. If you are using it intentionally, then try to sort out your constraints so they don't conflict with each other.