从 UIScrollView Swift 禁用水平滚动

Disabling Horizontal Scrolling from UIScrollView Swift(从 UIScrollView Swift 禁用水平滚动)
本文介绍了从 UIScrollView Swift 禁用水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

滚动视图

我有一个 UIScrollView,带有约束 left: 0, top: 0, right: 0, bottom: 0

I have a UIScrollView, with constraints left: 0, top: 0, right: 0, bottom: 0

内部滚动视图

在这个 UIScrollView 的顶部是一个 UIImageView 约束 left: 0, top: 0, right: 0, height: 200

At the top of this UIScrollView is a UIImageView with constraints left: 0, top: 0, right: 0, height: 200

在下面我有一个 UITextView 约束 left: 0, top: 0, right: 0, bottom: 0

Underneath this I have a UITextView with constraints left: 0, top: 0, right: 0, bottom: 0

这意味着 UITextView 将根据其内容调整大小,我为 UITextView<将 scrollingEnabled 设置为 false/代码>.

This means the UITextView will resize with respect to its content, and I set the scrollingEnabled to false for the UITextView.

所以,当我跑步时,它几乎完美运行.

So, when I run, it almost works perfectly.

其中一个问题是 UIImageView 占用的空间比实际屏幕宽度多 10%.因此,启用了水平滚动.

The one problem is the UIImageView takes up about 10% more than the actual screen width. Hence, horizontal scrolling is enabled.

我已尝试添加行

imageView.frame = CGRect(0, 0, screenSize.width, 200)
scrlView.contentSize.width = screenSize.width

但这没有区别.我仍然可以水平滚动,并且图像视图仍然比实际屏幕宽度多出 10% 左右.

but this makes no difference. I can still scroll horizontally and the Image View still takes up around 10% more than the actual screen width.

注意,我没有在情节提要中设置 imageView 屏幕宽度,仅以编程方式设置.

Note, I have not set imageView screen width in storyboard, only programatically.

有什么想法吗?

推荐答案

这样,

Swift 4.0

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.x>0 {
        scrollView.contentOffset.x = 0
    }
}

而且,你可以设置这个属性:

And, you can set this property:

scrollImg.isDirectionalLockEnabled = true

这篇关于从 UIScrollView Swift 禁用水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
Resume game cocos2d(恢复游戏 cocos2d)
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)