在UIScrollView Swift中禁用垂直滚动 [英] Disable vertical scroll in UIScrollView Swift

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

问题描述

(不要将此问题标记为重复.我读了很多问题,但找不到我问题的答案.)

(Don't mark this question as duplicated. I read a lot of questions but I don't find the answer to my issue.)

我的问题如下:我有一个 UIScrollView ,它应该只能水平滚动.在这里,它垂直滚动20像素(奇怪的事实:它看起来与状态栏的高度相同).

My issue is the following: I have a UIScrollView which is supposed to only scroll horizontally. Here it scrolls vertically for 20px (strange fact: it appears to be the same height of the status bar).

我试图像这样打印 UIScrollView 的内容大小,y偏移和高度:

I tried to print the content size, the y offset and the height of the UIScrollView like this:

print(horizontalScrollView.contentSize)
print(horizontalScrollView.bounds.size.height)
print(horizontalScrollView.contentOffset.y)

我明白了:

(1125.0, 667.0). // scrollview content size
667.0.           // scrollview height
-20.0            // content offset (y)

我知道 scrollView 内容大小的高度必须为< = scrollView 高度.如您所见,就是这种情况.

I know that the scrollView content size height must be <= scrollView height. And as you can see this is the case.

我使用约束条件将3个 UIViewController 水平堆叠.除此问题外,它运作良好.

I use constraints to put 3 UIViewController stacked horizontally. And it works well except of this issue.

另一个事实:它在Swift 4和Xcode 9之前有效...我不知道这是否可能是原因.

Another fact: It worked before Swift 4 and Xcode 9... I don't know if this could be the cause or not.

您有什么想法吗?在此先感谢您的帮助.

Do you have some ideas? Thanks in advance for your help.

推荐答案

由于iOS 11的内容插图可以自动调整.尝试将滚动视图的行为设置为不对其进行调整.

Since iOS 11 content insets can get adjusted automatically. Try setting your scrollview's behaviour to not adjust it.

if #available(iOS 11, *) {
    scrollview.contentInsetAdjustmentBehavior = .never
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆