代码之家  ›  专栏  ›  技术社区  ›  SpaceX

将图像大小限制为ScrollView,Swift

  •  0
  • SpaceX  · 技术社区  · 7 年前

    如何将图片限制在滚动视图中。

    imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight-50)
    imageScrollView.clipsToBounds = true // Has no affect on the image
    

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  3
  •   Xavier L.    7 年前

    您是否引用了UIImageView?如果是这样,则将其内容模式设置为aspect fit。这样地:

            theImageView.contentMode = .scaleAspectFit
    

    Interface Builder, baby!


    那么,如果您没有对UIImageView的引用呢?。。。 您可以遍历滚动视图的子视图,每当它找到UIImageView时,您可以这样设置内容模式。类似于:

    //This is off the top of my head, so my filtering may not be right...
    //This is also a one and done solution if you've got a lot of images in your scroll view
    for anImgVw in imageScrollView.subviews.filter({$0.isKind(of: UIImageView.self)})
    {
        anImgVw.contentMode = .scaleAspectFit
    }
    

    否则,我不确定如果不引用UIImageView是否可行。

        2
  •  1
  •   DonMag    7 年前

    您使用的库编码为使缩放与设备方向相匹配。那么,如果 方向与 看法 方向,您最终会发现图像在滚动视图中不太合适。

    您需要编辑 ImageScrollView.swift 源文件。假设您使用的版本与当前提供的链接中的版本相同( https://github.com/huynguyencong/ImageScrollView ),更改 setMaxMinZoomScalesForCurrentBounds() 功能如下:

    fileprivate func setMaxMinZoomScalesForCurrentBounds() {
        // calculate min/max zoomscale
        let xScale = bounds.width / imageSize.width    // the scale needed to perfectly fit the image width-wise
        let yScale = bounds.height / imageSize.height   // the scale needed to perfectly fit the image height-wise
    
        // fill width if the image and phone are both portrait or both landscape; otherwise take smaller scale
        //let imagePortrait = imageSize.height > imageSize.width
        //let phonePortrait = bounds.height >= bounds.width
        //var minScale = (imagePortrait == phonePortrait) ? xScale : min(xScale, yScale)
        //
        // just take the min scale, so the image will completely fit regardless of orientation
        var minScale = min(xScale, yScale)
    
        let maxScale = maxScaleFromMinScale*minScale
    
        // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
        if minScale > maxScale {
            minScale = maxScale
        }
    
        maximumZoomScale = maxScale
        minimumZoomScale = minScale * 0.999 // the multiply factor to prevent user cannot scroll page while they use this control in UIPageViewController
    }
    
        3
  •  0
  •   sarosh mirza    7 年前

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height
    
    imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: screenHeight-50)