UIScrollView 和约束的问题 [英] Trouble with UIScrollView and Constraints

查看:52
本文介绍了UIScrollView 和约束的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 UIScrollViewConstraints 时遇到问题.

I'm having trouble with UIScrollView and Constraints.

我想在我的应用程序中显示一个FAQ.这将包括几个问题和答案,长度各不相同.它在发布后不会改变,所以我直接在故事板中处理所有事情.但是,它必须适用于不同的 iPhone 屏幕尺寸(从 iPhone 5 到 iPhone 6 Plus).

I want to display a FAQ inside my application. This will include several questions and answers, with various length. It won't change after launch, so I handle everything directly in the storyboard. However, it has to work on different iPhone screen size (from iPhone 5 to iPhone 6 Plus).

所以我想做的是:

UIView
  UIScrollView
     UIView (FAQ1)
        UILabel (Question 1)
        UITextView (Answer 1)
     UIView (FAQ2)
        UILabel (Question 2)
        UITextView (Answer 2)
     ...

以下是它的外观示例(故意使用颜色,在运行时查看):

Here is an example of what it looks like (color on purpose, to see at runtime) :

现在,如果我应用一些约束,我会设法做到:

Now if I apply some constraints, I manage to get that :

但是它不能滚动并且文本在右侧.

However it is not scrollable and the text is out on the right side.

如果我在较小的屏幕上运行同样的事情,我会得到:

If I run the same thing on a smaller screen, I get that :

这次是可滚动的(不超过您在屏幕截图上看到的内容...),但文本仍未显示.

Which is scrollable this time (not more than what you see on the screenshot...) and the text is still out.

有没有人可以帮助我理解如何通过 UIScrollView 正确使用 Constraints ?

Is there anyone who could help me understanding how to use Constraints properly with an UIScrollView ?

如果可能,文本大小必须在小屏幕上缩小.如果不是,它应该保持相同的大小,但 UITextView 也将是可滚动的.

If possible the text size has to shrink on small screens. If it isn't, it should keep the same size but the UITextView will be scrollable too.

推荐答案

使用滚动视图和自动布局的最佳方式是在滚动视图内部添加一个视图,并将其所有边缘固定到滚动视图和等宽到超级视图,固定所有滚动视图的边缘到超级视图

The best way to use scrollview and auto layout is to add a view inside the scrollview and pin all its edges to scroll view and equal widths to superview, Pin all edges of scrollview to superView

  1. 将标签的顶部、左侧、右侧和高度固定到常见问题解答 1 视图中.
  2. 将文本视图的顶部固定到标签,将左右和底部固定到 FAQ1 视图.
  3. 将 FAQ1 视图的高度以及 faq1 视图的左右固定到内容视图.
  4. 按照第 1 步到第 3 步查看您的所有常见问题解答.
  5. 将 faq1 视图的顶部固定到内容视图的顶部
  6. 将下一个常见问题视图的顶部固定到上一个常见问题视图的底部.
  7. 将最后一个常见问题视图的底部固定到内容视图的底部

当添加您的 faq1 视图时,将其顶部到顶部、左侧和右侧固定到 contentView 的左上角和右上角,并带有必要的填充.

when adding your faq1 view pin its top to top ,left and right to contentView's top left and right with necessary padding.

您可以在此处找到示例代码希望这能解决您的问题

You can find the sample code here Hope this solves your problem

这篇关于UIScrollView 和约束的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆