故事板中的 UIScrollView 不能与 iOS 8 大小类和自动布局一起使用

时间:2022-10-27
本文介绍了故事板中的 UIScrollView 不能与 iOS 8 大小类和自动布局一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我试图只在情节提要中创建一个 UIScrollView,它允许我添加超过 VC 高度的滚动标签.这是我所做的:

So I'm trying to create a UIScrollView only in storyboard that allows me to add scrolling labels for more than the height of the VC. Here's what I did:

  1. 创建的UIScrollView占据任意宽度任意高度VC的大小

  1. Created UIScrollView that took up the size of the any width any height VC

在顶部、底部、左侧和右侧设置了与最近邻居的间距为 0

Made constraints 0 for spacing to nearest neighbor on top, bottom, left, and right

创建了一个 UIScrollViewsubView 视图,其宽度与任意宽度任意高度 VC 相同,但高度为 1500(因为我只想要它垂直滚动).

Created a view that is a subView of the UIScrollView with the same width as the any width any height VC but height of 1500 (because I only want it to scroll vertically).

将最近邻的约束设置为 0,仅用于左侧、顶部和右侧,并将高度约束设置为 1500.

Set constraints to nearest neighbor as 0 for ONLY left, top, and right and set the height constraint as 1500.

我在subView的顶部和底部放了一个标签

I put a label at the top of the subView and at the bottom

当我在 iPhone 6 上运行该应用程序时,不会像我想要的那样垂直滚动.任何想法为什么这不起作用?提前致谢.

When I run the app on an iPhone 6, does not scroll vertically as I want it to. Any ideas why this is not working? Thanks in advance.

推荐答案

要获得滚动,您必须将子视图(内容视图")固定到滚动视图的顶部、左侧、底部和右侧.此外,您必须给它(给内容视图")一个明确的(=与滚动视图无关)高度和宽度,因为滚动视图使用这些来计算其内容大小.在您的情况下,将宽度设置为 VC 主视图宽度,将高度设置为 1500.当宽度或高度大于滚动视图大小时,它将滚动.

To obtain the scroll you have to pin the sub view (the "content view") to the top, left, bottom and right of the scrollview. In addition you have to give it (to the "content view") an explicit (=not related to the scroll view) height and width because these are used by the scrollview to calculate its content size. In your case set the width equal to the VC main view width and the height to 1500. When width or height are bigger than the scrollview size, it will scroll.

滚动视图约束(固定到主视图)

Scroll view constraints (pinned to main view)

内容视图(固定到滚动视图 + 高度 1500,宽度=主视图宽度)+ 标签约束(例如:内容视图左上角 20 20)

Content view (pinned to scrollview + height 1500, width=mainview width) + label constraints (as an example: 20 20 from content view top left)

这篇关于故事板中的 UIScrollView 不能与 iOS 8 大小类和自动布局一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:如何继承 UIScrollView 并使委托属性私有 下一条:以编程方式将 UIPageControl 链接到 UIScrollView

相关文章

最新文章