【问题标题】:Is it possible to have a UIImageView inside of a UIScrollView zoom in and out, but stick to center with Auto Layout?是否可以在 UIScrollView 内放大和缩小 UIImageView,但坚持使用自动布局居中?
【发布时间】:2026-01-08 13:40:02
【问题描述】:

长话短说,我正在尝试构建类似于 Photos.app 的功能。

我在 Storyboard 中设置了一个 UIScrollView,里面有一个 UIImageView。缩放有效,但我无法保持居中。在我所有基于框架的滚动视图实现中,我将其居中如下,效果很好:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGRect newImageViewFrame = self.imageView.frame;

    // Center horizontally
    if (newImageViewFrame.size.width < CGRectGetWidth(scrollView.bounds)) {
        newImageViewFrame.origin.x = (CGRectGetWidth(scrollView.bounds) - CGRectGetWidth(self.imageView.frame)) / 2;
    }
    else {
        newImageViewFrame.origin.x = 0;
    }

    // Center vertically
    if (newImageViewFrame.size.height < CGRectGetHeight(scrollView.bounds)) {
        newImageViewFrame.origin.y = (CGRectGetHeight(scrollView.bounds) - CGRectGetHeight(self.imageView.frame)) / 2;
    }
    else {
        newImageViewFrame.origin.y = 0;
    }

    self.imageView.frame = newImageViewFrame;
}

但对于自动布局,它根本不是。

我对 UIScrollView 或 UIImageView 没有任何限制,因为我不知道它们应该是什么。我在想我应该把 UIScrollView 粘到四个角上,但是对于 UIImageView 我不完全确定,因为缩放会改变它的框架。

这是一个示例项目:http://cl.ly/21371H3q381N

如何使用自动布局进行居中缩放?

【问题讨论】:

  • 检查这个答案*.com/a/14553055/1597744它会帮助你。
  • 嗯,你是怎么解决这个问题的?我正在努力使用 Autolayout 实现相同的功能。
  • @BlackFlam3 改为使用框架。

标签: ios objective-c uiscrollview uiimageview autolayout


【解决方案1】:

当使用 Autolayout 时,对 setFrames 的调用没有生效,这就是为什么 imageView 不在你的 scrollView 中居中。

话虽如此,为了达到中心效果,您可以选择:

  1. 最简单的方法是将translatesAutoresizingMaskIntoConstraints 设置为YESViewDidLoad 中的imageView,这会将对setFrame: 的调用转换为基于您的imageView autoresizingMask 的新约束。但是您应该确保新约束满足您在情节提要中设置的约束(在您的情况下没有)。

  2. 在您的 scrollViewDidZoom: 方法中直接添加约束以使您的 imageView 居中

-

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

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

【讨论】:

    【解决方案2】:

    要使内容居中,可以参考这段代码 sn-p:

    // To re-center the image after zooming in and zooming out
    - (void)centerScrollViewContents
    {
     CGSize boundsSize = self.bounds.size;
     CGRect contentsFrame = self.imageView.frame;
    
     if (contentsFrame.size.width < boundsSize.width)
     {
       contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
     }
     else
     {
      contentsFrame.origin.x = 0.0f;
     }
    
     if (contentsFrame.size.height < boundsSize.height)
     {
      contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
     }
     else
     {
        contentsFrame.origin.y = 0.0f;
     }
    
     self.imageView.frame = contentsFrame;
    }
    
    //for zooming in and zooming out
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView
     {
      if (self.zoomScale > 1.0f)
      {
        [self centerScrollViewContents];
      }
       else
      {
      self.bouncesZoom    =   NO;
    
        // for zooming out by pinching
      [self centerScrollViewContents];
      }
    
     }
    

    这样,您可以在不使用 AutoLayout 的情况下放大和缩小图像后重新定位图像的坐标。如果有帮助,请告诉我。谢谢:)

    【讨论】:

      最近更新 更多