ios - 如何将 UIScrollView 中的内容设置在中间,以便用户可以在 iOS 中左右滚动

标签 ios objective-c

我有一个关于 UIScrollView 的问题。我想在 UIScrollView 中间设置内容,这样用户可以轻松地左右水平滚动。UIScrollView 的 ContentSize 应该是内容的 3 倍。下图是:-

|--------------------------------UIScrollView的ContentSize---------- --------------------------|

                |-----------content----------|

如何设置?

谢谢!

最佳答案

我知道你想要这样的东西:

|----scrollVIew.frame.size.width---|

|----------------scrollVIew.contentSize.width------------------|

                 |-content.frame.size.width-|

其中 scrollVIew.contentSize.width = 3 x content.frame.size.width。

要执行类似的操作,首先,设置您的 scrollview.frame:

scrollView.frame = CGRectMake(0, 0, width, height);

然后创建内容 View 并将其添加到您的 scrollView 中并设置其框架:

CGFloat contentViewWidth = 200.f; // the width of your contentView
contentView.frame = CGRectMake(contentViewWidth, 0, contentViewWidth, height);

最后,设置 ScrollView 的内容大小:

scrollView.contentSize = CGSizeMake(contentViewWidth * 3, height);

如果你想让内容居中,你可以手动滚动到你的scrollVIew的中间:

[scrollView setContentOffset:CGPointMake(scrollView.contentSize.width * 0.5f - width * 0.5f, 0)];

关于ios - 如何将 UIScrollView 中的内容设置在中间,以便用户可以在 iOS 中左右滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27230945/

相关文章:

ios - 如何将大字符串传递给 iMessage 扩展?

objective-c - 处理游戏中心通知

iphone - 为什么我们调用 doesNotRecognizeSelector : method?

objective-c - 为什么 UISplitViewController 不能是 UIWindow 的 rootViewController 属性?

objective-c - 在 Objective-C 中存储和检索数字对的快速方法

iphone - iOS 设备中的图标 100x100 命名约定

objective-c - iOS静态 TableView 不那么静态

objective-c - 在 Swift 中使用 PDFKIT 搜索 PDF

ios - 当我在 Xcode 中运行应用程序时,按钮字体与 Storyboard字体不匹配

ios - 无法识别的选择器发送到 NSKeyedUnarchiver 实例