ios 从 UIScrollView Swift 禁用水平滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32197630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling Horizontal Scrolling from UIScrollView Swift
提问by Greg Peckory
Scroll View
滚动视图
I have a UIScrollView, with constraints left: 0, top: 0, right: 0, bottom: 0
我有一个 UIScrollView,有约束 left: 0, top: 0, right: 0, bottom: 0
Inside Scroll View
内部滚动视图
At the top of this UIScrollView
is a UIImageView
with constraints left: 0, top: 0, right: 0, height: 200
在这个顶部UIScrollView
是一个UIImageView
有约束的left: 0, top: 0, right: 0, height: 200
Underneath this I have a UITextView
with constraints left: 0, top: 0, right: 0, bottom: 0
在这之下我有一个UITextView
约束left: 0, top: 0, right: 0, bottom: 0
This means the UITextView
will resize with respect to its content, and I set the scrollingEnabled
to false
for the UITextView
.
这意味着UITextView
将针对其内容调整大小,我设置scrollingEnabled
到false
了UITextView
。
So, when I run, it almostworks perfectly.
因此,当我运行时,它几乎可以完美运行。
The one problem is the UIImageView
takes up about 10% more than the actual screen width. Hence, horizontal scrolling is enabled.
一个问题是UIImageView
它比实际屏幕宽度多出 10% 左右。因此,启用了水平滚动。
I have tried adding the lines
我试过添加行
imageView.frame = CGRect(0, 0, screenSize.width, 200)
scrlView.contentSize.width = screenSize.width
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.
但这没什么区别。我仍然可以水平滚动,图像视图仍然比实际屏幕宽度多 10%。
Note, I have not set imageView screen width in storyboard, only programatically.
请注意,我没有在故事板中设置 imageView 屏幕宽度,只是以编程方式设置。
Any ideas?
有任何想法吗?
回答by Loxx
Like this,
像这样,
Swift 4.0
斯威夫特 4.0
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.x>0 {
scrollView.contentOffset.x = 0
}
}
And, you can set this property:
而且,您可以设置此属性:
scrollImg.isDirectionalLockEnabled = true
回答by the_dude_abides
I changed this so that it just returns 0. No need to check at all if you want scroll off.
我改变了这一点,使它只返回 0。如果你想滚动关闭,根本不需要检查。
func scrollViewDidScroll(scrollView: UIScrollView) {
scrollView.contentOffset.x = 0
}
No need for the directional lock.
不需要方向锁。
回答by HunterCopp
Swift 4
斯威夫特 4
Horizontal Scroll Lock
水平滚动锁定
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x != 0 {
scrollView.contentOffset.x = 0
}
}
You can change the x to y for vertical scrolling.
您可以将 x 更改为 y 以进行垂直滚动。
Make sure to add UIScrollViewDelegate like this:
确保像这样添加 UIScrollViewDelegate:
class MyViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
...
}
And set the delegate for the ScrollView
并为 ScrollView 设置委托
scrollView.delegate = self
回答by Steven Lee
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.x = 0
}
This stops the scrollview from scrolling towards the leading edge too.
这也阻止了滚动视图向前沿滚动。
回答by Rohit Kumar
If you are using storyboard and autolayout, then you should consider how ScrollView work in Storyboard with autolayout.
如果您正在使用故事板和自动布局,那么您应该考虑 ScrollView 如何在具有自动布局的故事板中工作。
Consider the following.
考虑以下。
Add a single view on your scrollView with constrains left, right, top, bottom, height, width,
Make the outlet of the width and height
Add your subViews to the view you added in the scrollView
在您的 scrollView 上添加一个带有左、右、顶部、底部、高度、宽度约束的视图,
制作宽度和高度的出口
将您的子视图添加到您在滚动视图中添加的视图
update the width to the screenSize eg: 320 for iPhone5 or 4.
将宽度更新为 screenSize 例如:iPhone5 或 4 的 320。
self.viewWidth = SCREEN_WIDTH;
[self.view updateConstraints];