UIScrollView的滚动内容大小歧义 [英] UIScrollView Scrollable Content Size Ambiguity

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

问题描述

研究员开发者,
我有自动版式在Interface Builder(X code 5 / iOS的7)的麻烦。
这是非常基本的和非常重要的,所以我认为每个人都应该知道这是如何正确地工作。如果这是在X code进行错误,它的一个非常关键的一年!

所以,每当我有一个观点的层次结构,如这个,我遇到麻烦:


  

的UIViewController


  
  

    

的UIView


    
    

      

UIScrollView的


      
      

        

的UILabel(或任何其它类似的UIKit元件)


      
    
  

该UIScrollView中有着雄厚的限制,例如,从每边50像素(没问题)。
然后我一个顶层空间的限制(由于标签的固有大小,我可以连销高度/宽度的标签,改变不了什么,而应该是不必要的)添加到的UILabel(没问题)

麻烦的开始,当我增加一个尾部约束到的UILabel:

例如,尾随空格至:上海华等于:25

现在出现两次警告 - 我不明白为什么:

A)滚动内容大小歧义(滚动型有暧昧的滚动内容高度/宽度)

B)错放视图(标签应为:X = -67,实际:X = 207

我做这个小例子,在刚新的项目,你可以下载和我附上截图。正如你所看到的,Interface Builder中预计,标签到UIScrollView的边界(橙色虚线矩形)的坐在外面。与解决问题工具移动它那里更新标签的框架。

请注意:如果你有一个UIView更换的UIScrollView,该行为是按预期(标签的框架是正确的,并根据约束)。所以似乎要么是与UIScrollView的问题或我错过了一些重要的东西。

当我运行应用程序,而不更新标签的框架由IB的建议它被定位得很好,它的确切位置应该是与UIScrollView的是滚动的。
如果我做更新帧上的标签是淡出人们的视线和的UIScrollView不滚动。

帮助我欧比旺·克诺比!为什么暧昧布局?为什么错位的观点???

您可以在这里下载示例项目和尝试,如果你能弄清楚是怎么回事:
<一href=\"https://github.com/Wirsing84/AutoLayoutProblem\">https://github.com/Wirsing84/AutoLayoutProblem


解决方案

所以,我只是整理出来是这样的:


  1. 添加的UIScrollView A 的UIView (我们可以称之为内容查看);


  2. 在此内容查看设置上,下,左,右页边距为0 (从<$ C $历程C>滚动视图这是上海华); 设置也居中对齐水平和垂直


完成

现在,您可以添加在你所有的意见内容查看 contentSize >滚动视图将根据自动调整大小的内容查看

更新:

//计算器:

一些特殊情况下,由该影片发表的 @塞尔吉奥在下面的意见。

Fellow devs, I am having trouble with AutoLayout in Interface Builder (Xcode 5 / iOS 7). It's very basic and its very important so I think everyone should know how this properly works. If this is a bug in Xcode, its a very critical one!

So, whenever I have a view hierarchy such as this I run into trouble:

UIViewController

UIView

UIScrollView

UILabel (or any other comparable UIKit Element)

The UIScrollView has solid constraints, e.g., 50 px from every side (no problem). Then I add a Top Space constraint to the UILabel (no problem) (and I can even pin height / width of the label, changes nothing, but should be unneccessary due to the Label's intrinsic size)

The trouble starts when I add a trailing constraint to the UILabel:

E.g., Trailing Space to: Superview Equals: 25

Now two warnings occur - and I don't understand why:

A) Scrollable Content Size Ambiguity (Scroll View has ambiguous scrollable content height / width)

B) Misplaced Views (Label Expected: x= -67 Actual: x= 207

I did this minimal example in a freshly new project which you can download and I attached a screenshot. As you can see, Interface Builder expects the Label to sit outside of the UIScrollView's boundary (the orange dashed rectangle). Updating the Label's frame with the Resolve Issues Tool moves it right there.

Please note: If you replace the UIScrollView with a UIView, the behaviour is as expected (the Label's frame is correct and according to the constraint). So there seems to either be an issue with UIScrollView or I am missing out on something important.

When I run the App without updating the Label's frame as suggested by IB it is positioned just fine, exactly where it's supposed to be and the UIScrollView is scrollable. If I DO update the frame the Label is out of sight and the UIScrollView does not scroll.

Help me Obi-Wan Kenobi! Why the ambiguous layout?? Why the misplaced view???

You can download the sample project here and try if you can figure out what's going on: https://github.com/Wirsing84/AutoLayoutProblem

解决方案

So I just sorted out in this way:

  1. Add in the UIScrollView a UIView (we can call that contentView);

  2. In this contentView, set top, bottom, left and right margins to 0 (of course from the scrollView which is the superView); Set also align center horizontally and vertically;

Finished.

Now you can add all your views in that contentView, and the contentSize of the scrollView will be automatically resized according with the contentView.

Update:

Some special case is covered by this video posted by @Sergio in the comments below.

这篇关于UIScrollView的滚动内容大小歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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