如何为 UIScrollView 启用方向锁定?

时间:2022-12-29
本文介绍了如何为 UIScrollView 启用方向锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIScrollView,里面有一个 UIView.我想锁定 x 轴,以便视图仅垂直滚动.如何启用定向锁定?

I have a UIScrollView with a UIView inside. I want to lock the x-axis so that the view is only scrolled vertically. How do I enable directional locking?

推荐答案

首先,设置UIScrollViewcontentSize的宽度等于或小于UIScrollView 框架的宽度.

First, set the UIScrollView's contentSize to have a width that is equal to or less than the width of the UIScrollView's frame.

接下来,将 UIScrollView's alwaysBounceHorizo​​ntal 设置为 NO.这将防止滚动视图出现橡皮筋",即使您告诉它没有更多的水平内容要显示.

Next, set UIScrollView's alwaysBounceHorizontal to NO. This will prevent the scroll view from "rubber banding" even though you've told it there's no more horizontal content to display.

UIScrollView *scrollView;
CGSize size = scrollView.contentSize;
size.width = CGRectGetWidth(scrollView.frame);
scrollView.contentSize = size;
scrollView.alwaysBounceHorizontal = NO;

滚动视图中的实际内容并不重要.

It doesn't matter what's actually inside the scroll view.

Swift 5.0

let scrollView = UIScrollView() // Or however you want to initialize it
var size = scrollView.contentSize
size.width = scrollView.frame.width
scrollView.contentSize = size
scrollView.alwaysBounceHorizontal = false

这篇关于如何为 UIScrollView 启用方向锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:UIScrollView 不会滚动! 下一篇:UITextView 文字在高度达到 8192.0 后变得不可见

相关文章

最新文章