在自动布局的情况下,滚动型IOS暧昧的滚动内容高度 [英] IOS scrollview ambiguous scrollable content height in case of autolayout

查看:167
本文介绍了在自动布局的情况下,滚动型IOS暧昧的滚动内容高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在开发IOS的小应用程序中,我使用滚动视图,支持自动滚动layout.Inside我增加了两个观点。我使用IB和自动布局约束。我陆续在垂直的方式增加在侧面有一个两个视图。我已经加入外约束像尾随,领导,顶部,底部的空间。我还添加了高度限制为两种观点。直到这一切工作正常。

Hi I am developing small IOS application in which I am using scrollview with auto-layout.Inside scroll I am adding two more views. I am using IB and auto-layout constraints. I am adding two views in side one after another in vertical manner. I have added outer constraints like trailing, leading, top, bottom space. I also added height constraints for both views. Till this everything is working fine.

但我的厂景有一些动态内容。出于这个原因我想使高度约束大于等于代替更大,以等于。

But my view1 has some dynamic content. For that reason I want to make height constraint greater than equal to instead of equal to.

那么如何解决这个问题。需要一些帮助。谢谢你。

So how to resolve this problem. Need some help. Thank you.

推荐答案

您应该遵循下面的方法。
首先,这里有大约滚动查看一些重要的事情是自动布局重要的:

You should follow the approach below. First of all, here are some important things about scroll Views which are important for auto layout:


  1. 的UIScrollView 自动改变其边界。

  2. 的UIScrollView 需要获取的内容大小滚动内容视图(UI),它可以很好地支持自动布局。

  3. 的UIScrollView 的顶部和底部的约束应该连接到顶部和底部布局指南(对于大多数的情况下,不是所有)。

  1. UIScrollView changes its bounds automatically.
  2. UIScrollView needs a content View(in UI) for getting content size for scrolling which works smoothly for auto layout.
  3. UIScrollView's top and bottom constraint should connected to top and bottom layout guide (For most of the cases, not all).

根据您的问题:

第一种方法:你有的UIScrollView ,所以只需插入一个的UIView 里面,并把它当作内容的浏览。之后,把你的两个 UIViews 中的的UIView (查看内容)。

First Approach: You have UIScrollView, so just insert one UIView inside it and consider it as Content View. After that put your two UIViews inside the UIView (Content View).

所以层次是:的MainView - > UIScollView - > 的UIView (内容查看) - >的firstView&安培;第二种观点。现在我们要给约束他们。

So the Hierarchy is: MainView --> UIScollView --> UIView (ContentView) --> firstView & Second View. Now we are going to give constraints to all of them.


  • 对于的UIScrollView 顶部和底部的限制连接到顶部和放大器;底部布局指南和前导和尾随到主视图。

  • 对于的UIView (查看内容)是非常重要的给约束领导,拖尾,顶部,底部的UIScrollView的,并给予明确的高度(正常高度约束)您的内容查看哪个适合滚动(例如1200)。也使其在容器中水平居中。

  • 现在,给约束你的第一个观点:前置,TOP到内容查看,并给予明确的高度(正常身高的限制)。不要试图马上修改它在大于等于 - 我们稍后会做到这一点。然后,给予约束,以第二种观点前导,尾随到内容查看,上到的firstView,下到内容视图和显式高度(你想要的话)。现在,试图从修改第一个视图的高度约束等于 - >大于等于 - 它肯定会工作

  • For UIScrollView, connect TOP and BOTTOM constraints to TOP & Bottom Layout Guide and LEADING and TRAILING to the Main View.
  • For UIView (Content View) it is very important to give constraints LEADING, TRAILING, TOP, BOTTOM to the UIScrollView and to give the explicit height (normal height constraint) to your contentView which is appropriate for scrolling (e.g 1200). Also make it horizontally center in container.
  • Now give constraints to your first view: LEADING, TRAILING, TOP to ContentView and give explicit height (normal height constraint). Don't try to modify it in greater than equal right now - we will do this later. Then, give constraint to second view Leading, Trailing to ContentView, Top to FirstView, Bottom to Content View and Explicit Height (what ever you want). Now, try to modify the Height constraint of first view from equal -> greater than equal - it will definitely work.

第二个方法(容易和简单):给予约束根据第一点,对于改变第一观的高度动态的,您可以创建第一个视图的高度约束的一个IBOutlet到类,并根据您的要求,您可以更改约束的后恒定值(按照你想第一个视图的高度)以任何方法或按钮动作,因此将在运行时更改。你也可以考虑它是当你要隐藏你的意见,只是改变他们的高度约束常数为0,所以它会隐藏和取消隐藏的时间,相同的恒定值重新设定期望值罚款招,所以你也可以与隐藏和观点取消隐藏功能,这是从其他途径自动布局有点吃力轻松播放。

Second Approach (Easy and Simple): After giving constraint as per first point, for changing height of first View dynamically you can create an IBOutlet of height constraint of first view to your class and as per your requirement you can change the constraint's constant value (as per you want the height of first view) in any method or button action so it will change in run time. You can also consider it is a fine trick when you want to hide your views so just change their Height constraint's constant to 0 so it will hide and at the time of unhide, again set the constant value of same to desired value so you can also easily play with hide and unhide functionality of view which is little bit difficult in auto layout from other ways.

这篇关于在自动布局的情况下,滚动型IOS暧昧的滚动内容高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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