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

问题描述

滚动视图

我有一个UIScrollView,约束左:0,顶部:0,右: 0,底部:0

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

内部滚动视图

顶部的UIScrollView UIImageView ,约束左:0,顶部:0,右:0,身高:200

在这下面我有一个 UITextView 约束左:0,上:0,右:0,下:0

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

这意味着 UITextView 将根据其内容调整大小,并将 scrollingEnabled 设置为 false 用于 UITextView

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.

注意,我没有在storyboard中设置imageView屏幕宽度,仅编程。

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

任何想法?

推荐答案

像这样,

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

并且,您可以设置此属性:

And, you can set this property:

scrollImg.directionalLockEnabled = true

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

08-16 04:24