How do I enable directional lock for a UIScrollView?(如何为 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?
推荐答案
首先,设置UIScrollView
的contentSize
的宽度等于或小于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
alwaysBounceHorizontal
设置为 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 启用方向锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 UIScrollView 启用方向锁定?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01