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

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

问题描述

各位开发者,我在 Interface Builder (Xcode 5/iOS 7) 中遇到了 AutoLayout 的问题.这是非常基本和重要的,所以我认为每个人都应该知道它是如何正常工作的.如果这是 Xcode 中的一个错误,那就是一个关键的错误!

因此,每当我有这样的视图层次结构时,我就会遇到麻烦:

>UIViewController>>界面视图>>>UIScrollView>>>>UILabel(或任何其他类似的 UIKit 元素)

UIScrollView 有可靠的约束,例如,每边 50 像素(没问题).然后我向 UILabel 添加一个顶部空间约束(没问题)(我什至可以固定标签的高度/宽度,什么都不改变,但由于标签的固有大小应该是不必要的)

当我向 UILabel 添加尾随约束时,问题就开始了:

例如,尾随空间为:Superview 等于:25

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

A) Scrollable Content Size Ambiguity(滚动视图具有不明确的可滚动内容高度/宽度)

B) 错位的视图(预期标签:x= -67 实际:x= 207

我在一个你可以下载的全新项目中做了这个最小的例子,并附上了截图.如您所见,Interface Builder 希望 Label 位于 UIScrollView 的边界(橙色虚线矩形)之外.使用解决问题工具更新标签的框架会将其移动到那里.

请注意:如果将 UIScrollView 替换为 UIView,则行为符合预期(标签的框架是正确的,并且符合约束).所以 UIScrollView 似乎有问题,或者我错过了一些重要的东西.

当我运行应用程序而不按照 IB 的建议更新标签的框架时,它的位置很好,正好在它应该在的位置,并且 UIScrollView 是可滚动的.如果我确实更新了框架,则 Label 不在视线范围内,并且 UIScrollView 不会滚动.

帮助我欧比旺克诺比!为什么是模棱两可的布局?为什么会出现错位的视图?

您可以在此处下载示例项目并尝试弄清楚发生了什么:涵盖了一些特殊情况://stackoverflow.com/users/520487/sergio">@Sergio 在下面的评论中.

Fellow devs, I am having trouble with AutoLayout in Interface Builder (Xcode 5 / iOS 7). It's very basic and important so I think everyone should know how this properly works. If this is a bug in Xcode, it is a 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. Inside the UIScrollView add 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天全站免登陆