如何使用自动布局来设置我的UIScrollView限制? [英] How can I use Autolayout to set constraints on my UIScrollview?

查看:167
本文介绍了如何使用自动布局来设置我的UIScrollView限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了两天的时间<一个尝试各种解决方案href=\"https://developer.apple.com/library/ios/technotes/tn2154/_index.html#//apple_ref/doc/uid/DTS40013309\">Mixed和纯自动布局方法来达到自动版式一个简单的滚动视图设置之前是什么,以及它现在官方 - 我一定是太愚蠢了。我在故事板大多设置此功能(当然,这只是事情是这样的)。

I have spent two days trying out the various solutions for Mixed and Pure Autolayout approaches to achieve what was a trivial scrollview setup prior to autolayout, and it's now official - I must be too stupid. I am setting this up mostly in Storyboard (well, it's just the way it is).

因此​​,这里是我的帮助请求。

So here's my plea for help.

Viewtree:

UIView
-UIView
-UIView
..-UIScrollview
...-UIButton
...-UIButton
...-UIButton

按钮都应该水平滚动(左到右,反之亦然)。可有人的的让我知道如何设置约束来实现这一目标使用纯自动布局???

The buttons are supposed to scroll horizontally (left to right and vice versa). Can someone please let me know how to set the constraints to achieve this using pure Autolayout???

-

我曾尝试混合的方法,像这样:

I have tried the mixed approach, like so:

UIView
- UIView
- UIView
..-UIScrollview
...-UIView (contentview)
....-UIButton
....-UIButton
....-UIButton

...并设置固定的宽度和高度约束内容查看 translatesAutoresizingMaskIntoConstraints 设置按照苹果公司的技术说明。按钮和滚动视图设置了使用限制。这得到了滚动视图滚动(耶),但很可惜,它滚动太远了!据我所知,滚动宽度不知何故从我设定的???一倍内容查看!???

...and setting fixed width and height constraints for the contentview and the translatesAutoresizingMaskIntoConstraints settings as per Apple's TechNote. The buttons and scrollview are set up using constraints. This gets the scrollview scrolling (yay) but alas, it scrolls too far! As far as I can tell, the scroll width is somehow doubled from what I set the contentview at???!!!???

我试过纯自动版式的方法为好,都与内容查看和不带。 所有的意见是 translatesAutoresizingMaskIntoConstraints = NO ,除了 self.view 。该按钮有固定的宽度/高度限制,并固定在滚动型的四边。没有滚动。

I tried the pure autolayout approach as well, both with contentview and without. All the views are translatesAutoresizingMaskIntoConstraints=NO, except for self.view. The buttons have fixed width/height constraints, and are pinned to all four edges of the scrollview. Nothing scrolls.

所以,我完全是困惑,为什么我不能让它正常工作。任何帮助深表AP preciated,如果您需要任何其他信息,请咨询!

So I am totally baffled why I can't get it to work correctly. Any help is much appreciated, and if you need any other info, please ask!

已更新截图与解决方案 -
buttonZ约束:

UPDATED Screenshot with solution - buttonZ constraints:

修改@杰米阿甘
因此该解决方案被证明是在最后一个按钮错尾随约束。而不是6441,我已经设置的值是负值,-6441。棘手的是,在设定故事板的价值的时候,有两种选择引脚工具栏:

EDIT @ Jamie Forrest So the solution turns out to be the wrong trailing constraint on the last button. Instead of 6441, the value I had set was negative, -6441. The tricky thing is, that when setting the value in storyboard, there are two options in the Pin toolbar:

当前画布值为负(导致没有滚动),和下面的选项为正(激活滚动)。这意味着我不傻,但至少半瞎我猜。的虽然,我的防御,是不是有点令人不安的是,X code没有显示的不正确的设置错误?

The Current Canvas Value is negative (leading to no scroll), and the option below is positive (activating scroll). This means I'm not stupid but at least half-blind I guess. Although, to my defense, isn't it somewhat disturbing that XCode doesn't show an error for the "incorrect" setting?

EDITED AGAIN
现在,这是有趣的...从-6441(无滚动)结尾的值更改为6441启用滚动。但是,我的老朋友了太多contentsize回来了,从而导致内容大小的两倍大,因为它应该是什么!以获得正确的内容滚动的解决方案是尾随约束设置为零!在故事板工作,但看着@Infinity詹姆斯code时,这并不明显,这是应该的。

EDITED AGAIN Now this is funny... changing the trailing value from -6441 (no scroll) to 6441 enabled scroll. But my old friend the "too much contentsize" was back, leading to a content size twice as large as what it should be! The solution to get the correct content scroll was to set the trailing constraint to ZERO! This is not obvious when working in Storyboard but looking at @Infinity James' code, it is what it should be.

推荐答案

这是很难看到确切的价值和你的限制设置为您已将其粘贴在这里,所以我不知道从看你的截图,你在哪里出了问题。

It's hard to see the exact values and setup of your constraints as you've pasted them here, so I'm not sure from looking at your screenshots where you have gone wrong.

在代替的什么是错在你的安装的解释,我创建了一个基本的示例项目一个非常类似的观点层次结构和约束设置你描述的那个。水平滚动将按预期在示例项目,它使用纯自动版式的做法,苹果描述了<一个href=\"https://developer.apple.com/library/ios/technotes/tn2154/_index.html#//apple_ref/doc/uid/DTS40013309\">Technical注意。

In lieu of an explanation of what's wrong in your setup, I've created a basic sample project with a very similar view hierarchy and constraint setup to the one you describe. The horizontal scrolling works as expected in the sample project, which uses the "Pure AutoLayout" approach that Apple describes in the Technical Note.

我也有很多原本让自动布局麻烦的UIScrollView 工作。它得到工作的关键是确保所有在滚动视图中的项目,加在一起的,有约束,最终链接到滚动视图的各个方面,而且有助于自动版式系统能够确定contentSize的滚动认为将超过它的帧更大。它看起来像你试图这样做,在你的code,但也许你曾在那里的一些多余的约束都使得contentSize太小了。

I also had a lot of trouble originally getting Auto Layout to work with UIScrollView. The key to getting it to work is making sure that all of the items in the scroll view, taken together, have constraints that eventually link to all sides of the scroll view and that contribute to the AutoLayout system being able to determine a contentSize for the scroll view that will be bigger than its frame. It looks like you were trying to do that in your code, but maybe you had some superfluous constraints in there that were making the contentSize too small.

另外值得注意的是,正如其他人提到的,自动版式和UIScrollView的,不再设置contentSize明确。该自动版式系统根据您的约束计算contentSize。

Also of note, as others mentioned, with AutoLayout and UIScrollview, you no longer set the contentSize explicitly. The AutoLayout System calculates the contentSize based on your constraints.

我也发现这本电子书章节是在让我了解这一切工作非常有帮助。希望这一切都有助于。

I also found this ebook chapter to be very helpful in making me understand how all this works. Hope all this helps.

这篇关于如何使用自动布局来设置我的UIScrollView限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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