iOS - 缩放时的中心滚动视图内容(使用自动布局)

时间:2016-03-09 18:57:08

标签: ios objective-c uiscrollview autolayout

在使用自动布局缩小时,我正试图将UIScrollView内容居中。

以下是我的代码现在的样子:

- (void)viewDidLoad
{
   UIView *scrollView = [self addScrollViewWithImageViewWithSuperView:self.view];
   //..... code for scrollview auto layout.......//
}

- (UIView *)addScrollViewWithImageViewWithSuperView:(UIView *)superview
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:false];
    [scrollView setMaximumZoomScale:4.0f];
    [scrollView setMinimumZoomScale:0.4f];
    [scrollView setDelegate:self];
    [scrollView setBackgroundColor:[UIColor whiteColor]];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo3.jpg"]];
    [imageView setTranslatesAutoresizingMaskIntoConstraints:false];
    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView setBackgroundColor:[UIColor redColor]];

    [superview addSubview:scrollView];

    [scrollView addSubview:imageView];

    [NSLayoutConstraint activateConstraints:@[
                                              [imageView.topAnchor constraintEqualToAnchor:scrollView.topAnchor constant:0],
                                              [imageView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor constant:0],
                                              [imageView.leftAnchor constraintEqualToAnchor:scrollView.leftAnchor constant:0],
                                              [imageView.rightAnchor constraintEqualToAnchor:scrollView.rightAnchor constant:0],
                                              [imageView.widthAnchor constraintEqualToAnchor:scrollView.widthAnchor],
                                              [imageView.heightAnchor constraintEqualToAnchor:scrollView.heightAnchor],
                                              ]];

    return scrollView;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return scrollView.subviews.firstObject;
}

以上代码效果很好。一切都按照我想要的方式到位,但当我缩小并且UIScrollView内容小于UIScrollView时,UIImageView会卡在左上角。

如何让UIImageViewUIScrollView为中心?

2 个答案:

答案 0 :(得分:0)

如果只设置imageView的中心等于scrollView的中心呢?

[scrollView addConstraint:
[NSLayoutConstraint constraintWithItem:imageView
                    attribute:NSLayoutAttributeCenterX
                    relatedBy:NSLayoutRelationEqual
                    toItem:scrollView
                    attribute:NSLayoutAttributeCenterX
                    multiplier:1.0
                    constant:0.0];

[scrollView addConstraint:
[NSLayoutConstraint constraintWithItem:imageView
                    attribute:NSLayoutAttributeCenterY
                    relatedBy:NSLayoutRelationEqual
                    toItem:scrollView
                    attribute:NSLayoutAttributeCenterY
                    multiplier:1.0
                    constant:0.0];    

答案 1 :(得分:0)

我找到了问题的答案!我改变了我的imageview的顶部和左边约束的常量。

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    [scrollView layoutIfNeeded];

    CGFloat offsetX = MAX((scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5, 0.0);
    CGFloat offsetY = MAX((scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5, 0.0);

    UIImageView *imageView = scrollView.subviews.firstObject;

    for (NSLayoutConstraint *constraint in imageView.superview.constraints)
    {
        if (constraint.firstAttribute == NSLayoutAttributeTop && constraint.secondAttribute == NSLayoutAttributeTop && constraint.firstItem == imageView && constraint.secondItem == imageView.superview)
        {
            constraint.constant = offsetY;
        }
        else if (constraint.firstAttribute == NSLayoutAttributeLeft && constraint.secondAttribute == NSLayoutAttributeLeft && constraint.firstItem == imageView && constraint.secondItem == imageView.superview)
        {
            constraint.constant = offsetX;
        }
    }
}