UIScrollView zooming with Auto Layout

后端 未结 3 2116
渐次进展
渐次进展 2020-12-08 14:12

I\'m trying to implement a UIScrollView the New Way, using Auto Layout. I\'ve set up constraints from the inner view to the scroll view so that it can compute i

3条回答
  •  庸人自扰
    2020-12-08 14:43

    The best answer that I have seen is Mark's (https://stackoverflow.com/users/1051919/mark-kryzhanouski), posted here: UIScrollView Zoom Does Not Work With Autolayout.

    The crux of it is that you have to anchor the image view that is nested in the scroll view, to the parent of the scroll view. Despite the guidance in the iOS 6 release notes, it is not intuitive to me what view is "floating" over what. In this case, the scrolling view is just a single image view.

    I did do a lot of experimentation with this, hoping to find an all-IB approach and found none. You can still generate the view hierarchy in IB, but you still have to programatically add constraints. You can delete some or all of the default constraints (mainly just to appease the constraint-conflict warnings), but you always need Mark's code to tie the image view to the parent of the scroll view, the grand-parent of the image view.

    It seems like it should be simpler than this - it "should just work" but:

    NSDictionary *viewsDictionary = @{ @"scrollView": self.scrollView, @"imageView": self.imageView };
    [self.view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"H:|[imageView(width)]"
        options:0
        metrics:@{@"width": @(self.imageView.image.size.width)}
        views:viewsDictionary]];
    
    [self.view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"V:|[imageView(height)]"
        options:0
        metrics:@{@"height": @(self.imageView.image.size.height)}
        views:viewsDictionary]];
    

提交回复
热议问题