Zooming UIImageView inside UIScrollView with autolayout

前端 未结 3 856
迷失自我
迷失自我 2020-12-14 08:50

I have a UIImageView embedded inside a UIScrollView, prior to iOS 6 and autolayout I used the following snippet inside the controller\'s viedDidLoad method to display a scro

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 09:32

    I also agree with Zsolt's suggestion and link.

    But I update the width/height constraints to allow it to handle any size image:

    - (void) initZoom
    {
      for (NSLayoutConstraint *constraint in self.photoImageView.constraints)
      {
        if (constraint.firstAttribute == NSLayoutAttributeWidth)
          constraint.constant = self.photoImageView.image.size.width;
        else if (constraint.firstAttribute == NSLayoutAttributeHeight)
          constraint.constant = self.photoImageView.image.size.height;
      }
    
      float minZoom = MIN(self.scrollView.bounds.size.width / self.photoImageView.image.size.width,
                          self.scrollView.bounds.size.height / self.photoImageView.image.size.height);
      if (minZoom > 1) return;
    
      self.scrollView.minimumZoomScale = minZoom;
    
      self.scrollView.zoomScale = minZoom;
    }
    

提交回复
热议问题