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

ios – 带有ImageView的UIScrollView不能垂直滚动,第1张

概述我有一个UI ImageView,它的图像大于屏幕尺寸.我有这个UIImageView作为UIScrollView的子视图.现在,我无法向下滚动.查看整个图像.但缩放功能正常. @IBOutlet weak var scrollView: UIScrollView!var imageView = UIImageView();override func viewDidLoad() { 我有一个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 – 带有ImageView的UIScrollView不能垂直滚动全部内容,希望文章能够帮你解决ios – 带有ImageView的UIScrollView不能垂直滚动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108350.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存