ios – 带有ImageView的UIScrollView不能垂直滚动

前端之家收集整理的这篇文章主要介绍了ios – 带有ImageView的UIScrollView不能垂直滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UI ImageView,它的图像大于屏幕尺寸.我有这个UIImageView作为UIScrollView的子视图.现在,我无法向下滚动.查看整个图像.但缩放功能正常.
@IBOutlet weak var scrollView: UIScrollView!

var imageView = UIImageView();

override func viewDidLoad() {
    super.viewDidLoad()
}

func centerScrollViewContents(){
    let boundsSize = scrollView.bounds.size
    var contentsFrame = imageView.frame

    if contentsFrame.size.width < boundsSize.width {
       contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2
    }
    else { 
       contentsFrame.origin.x = 0
    }

    if contentsFrame.size.height < boundsSize.height {            
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2
    }
    else {
        contentsFrame.origin.y = 0
    }

    imageView.frame = contentsFrame
   // scrollView.frame = contentsFrame        
}

func scrollViewDidZoom(scrollView: UIScrollView) {
    centerScrollViewContents()
}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return imageView
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {

    scrollView.delegate = self
    imageView.frame = CGRectMake(0,scrollView.frame.size.width,scrollView.frame.size.height)

   // imageView.contentMode = UIViewContentMode.ScaleAspectFill
   // imageView.clipsToBounds = true
    imageView.image = UIImage(named: imgstr)

    var imagee = UIImage(named: imgstr)
    let size = imagee?.size
   // imageView.frame = CGRectMake(0,size.width!,size.height!)
    imageView.contentMode = .Top
    scrollView.addSubview(imageView)

    scrollView.contentSize = size!
    let scrollViewFrame = scrollView.frame
    let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
    let minScale = min(scaleHeight,scaleWidth)

    scrollView.minimumZoomScale = 1
    scrollView.maximumZoomScale = 5
    scrollView.zoomScale = minScale

    centerScrollViewContents()   
}

解决方法

您必须解决UIScrollView工作的两个主要问题

>在初始化UIScrollView的所有数据的内部存在一个问题,我的意思是函数viewDidAppear.
>您必须为UIScrollView设置约束,如Top,Bottom,Left和Right为0,尊重您的SuperView.

如果将代码放在viewDidAppear内部的函数viewDidLoad中并删除函数viewDidAppear,它可以正常工作.喜欢以下方式:

override func viewDidLoad() {
    super.viewDidLoad()


    scrollView.delegate = self
    imageView.frame = CGRectMake(0,scrollView.frame.size.height)

    // imageView.contentMode = UIViewContentMode.ScaleAspectFill
    // imageView.clipsToBounds = true
    imageView.image = UIImage(named: "imgstr.jpg")

    var imagee = UIImage(named: "imgstr.jpg")
    let size = imagee?.size
    // imageView.frame = CGRectMake(0,scaleWidth)

    scrollView.minimumZoomScale = 1
    scrollView.maximumZoomScale = 5
    scrollView.zoomScale = minScale

    centerScrollViewContents()
}

您还必须在Storyboard中为UIScrollView设置约束

我认为问题在于以下区别:

> viewDidAppear在视图实际可见时被调用,并且可以在视图控制器的生命周期中多次调用(例如,当模式视图控制器被关闭并且视图再次可见时).您可以在此处执行任何布局操作或在UI中执行任何绘图 – 例如,呈现模态视图控制器.但是,你在这里做的任何事都应该是可重复的.最好不要在这里保留东西,否则你会得到记忆
如果在视图消失时不释放它们,则会泄漏.
>当视图控制器首次加载到内存中时,viewDidLoad只调用一次.这是您希望实例化任何实例变量并构建在此视图控制器的整个生命周期中存在的任何视图的位置.但是,此时通常尚未显示该视图.

如果您有任何问题,我有一个项目准备提交给Github,如果你想看到它.

UPDATE:
After test it again your code,I resume your error to less code. Your code work fine,if you put your line imageView.frame = CGRectMake(0,size!.width,size!.height) your code works fine without contraints.

像这样的东西:

override func viewDidAppear(animated: Bool) {


    scrollView.delegate = self        

    // imageView.contentMode = UIViewContentMode.ScaleAspectFill
    // imageView.clipsToBounds = true
    imageView.image = UIImage(named: "imgstr.jpg")

    var imagee = UIImage(named: "imgstr.jpg")
    let size = imagee?.size

    // This line should be here!!!
    imageView.frame = CGRectMake(0,size!.height)
    imageView.contentMode = .Top
    scrollView.addSubview(imageView)

    scrollView.contentSize = size!
    let scrollViewFrame = scrollView.frame
    let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
    let minScale = min(scaleHeight,scaleWidth)

    scrollView.minimumZoomScale = 1
    scrollView.maximumZoomScale = 5
    scrollView.zoomScale = minScale

    centerScrollViewContents()       

}

关键是在前面的代码中,您将图像的帧设置为scrollView的帧,而不是大于scrollView的图像大小.

我希望这对你有帮助.

猜你在找的iOS相关文章