简体   繁体   English

如何在UIScrollView上添加UIView

[英]How do I add UIView on UIScrollView

I wanted to add some labels and textfields on my view and that view should scroll, so I am thinking of putting view on scrollview. 我想在视图上添加一些标签和文本字段,并且该视图应该滚动,因此我正在考虑将视图放在scrollview上。

  1. I wanted to add some labels and textfields on my view 我想在视图上添加一些标签和文本字段
  2. And that view should scroll 该视图应滚动
  3. So I am thinking of putting scrollView 所以我正在考虑将scrollView
  4. And then I wanted to put my view having labels and Textfields 然后我想让我的视图具有标签和文本字段
  5. Is it possible? 可能吗?

Please check scrollview property Bounces Vertically is checked or not if not check it 请检查scrollview属性是否垂直反弹(如果未选中) 在此处输入图片说明

可能是您的内容少于ScrollView ,这就是禁用滚动的原因

Put view with labels in scrollview. 将带有标签的视图放在scrollview中。 Scrollview should be less in height than view. 滚动视图的高度应小于视图的高度。 View should have height considering all ui cvontrols in it. 考虑到其中的所有ui控件,视图应具有高度。 should work like this 应该像这样工作

Make sure your scroll view height is not greater than view its content size has to be greater as all people says. 确保滚动视图的高度不大于所有人都说的视图内容的大小。 You get confused between content size and scroll view height. 您会在内容大小和滚动视图高度之间感到困惑。 Meas using nib suppose your scroll view height is 300 and when you use the code content size should be greater than 480 so it will be scroll-able on iPhone 4 size device who's height is 480 使用笔尖进行测量时,假设滚动视图的高度为300,并且使用代码内容时大小应大于480,以便在高度为480的iPhone 4尺寸设备上可以滚动。

To make sure your constraints are well defined, first check these steps: 为了确保约束条件定义正确,请首先检查以下步骤:

  • Make sure your base view is a UIView 确保您的基本视图是UIView
  • Put into a UIScrollView and add constraints to the edges of UIView 放入UIScrollView并将约束添加到UIView的边缘
  • Put into the scrollview a UIView and add constraints to the edges of the scrollview. 将UIView放入滚动视图,并在滚动视图的边缘添加约束。 I call this "content view". 我称之为“内容视图”。
  • You should assign a width to the content view. 您应该为内容视图分配宽度。 I usually set the width of the content view equal to the width of the scrollview. 我通常将内容视图的宽度设置为等于滚动视图的宽度。
  • Put your labels/views/images/whatever into the content view and use autolayout constraints to resize them automatically to fit the target screen. 将标签/视图/图像/所有内容放入内容视图,并使用自动布局约束自动调整它们的大小以适合目标屏幕。

Please consider that: 请考虑:

  • Your scrollview must always have a height, fixed or dynamic. 您的滚动视图必须始终具有固定或动态的高度。 In order to avoid errors with autolayout, consider that: 为了避免自动布局错误,请考虑:
    • the last element on the bottom of the content view must always have a constraint to the bottom edge of the scrollview, or 内容视图底部的最后一个元素必须始终对滚动视图的底部边缘具有约束, 或者
    • the content view must have a fixed height 内容视图必须具有固定的高度
  • If the height of your content view is less than the height of the scrollview, the view will not scroll. 如果内容视图的高度小于滚动视图的高度,则该视图将不会滚动。 You should add more views or more margin to the bottom constraint of the content view. 您应该在内容视图的底部约束中添加更多视图或更多边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM