ios - 父 View 边界的最小 x 值?

标签 ios uiscrollview autolayout

这个表达式“父 View 边界的最小 x 值”在 Apple Documentation 中是什么意思?关于 UIScrollView 中的自动布局?

...some notes regarding Auto Layout support for UIScrollView:

  • In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.

  • The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.

1) 据我所知, View 的默认 bounds 是 (0, 0, width, height)。所以 x 是 0

2) 一个 View 怎么可能有更多 x 值以使其最小化?

最佳答案

任何 View 的自动布局约束通常与 View 的可见边界有关,但是当涉及到 UIScrollView 时, Autolayout 约束与 scrollView 的 contentView 有关。
很明显,因为 scrollView 是……应该滚动的。


假设 scrollViewcontentSize 是这样的:

myScrollView.frame = CGRectMake(0, 0, 100, 100);
myScrollView.contentSize = CGSizeMake(1000, 1000);

现在...
比如说,在这个 scrollView 中你有一个 subView,类似于:

UILabel *lblTest = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[lblTest setText:@"Where am I?"];
[myScrollView addSubView:lblTest];

滚动前:

  • myScrollView.contentOffset.y 将为 0
  • myScrollView.bounds.y 将为 0
  • myScrollView.frame.origin.y 将为 0
  • lblTest.frame.origin.y 将为 0(并且将可见)

滚动后(到底部):

  • myScrollView.contentOffset.y 将更改为 900
  • myScrollView.bounds.y900
  • myScrollView.frame.origin.y0
  • lblTest.frame.origin.y 将为 -900(并且不再可见)

这是 Apple 文档的意思:

The UIScrollView class scrolls its content by changing the origin of its bounds.

现在……
如果 Autolayout 与 UIScrollView 的可见边界有关,那么无论您滚动多少,UILabel 都不会向上滚动。
但是……
由于 UIScrollView 中的自动布局与 ScrollView 的 contentSize 相关,因此自动布局约束在其中与滚动功能相关。

所以...
当您在 scrollView 中为 subViews 使用自动布局时,它将与 scrollView 的实际大小相关,而不是可见大小。

关于ios - 父 View 边界的最小 x 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20298678/

相关文章:

ios - 小端位图 & "Unable to create GMSGLImageTile due to bad graphics data"

ios - 如何为带有部分的表格 View 单元格设置备用背景颜色

ios - 我的一个 UICollectionViews 没有滚动或响应我的 UIScrollView 容器内的触摸事件 [Puzzling]

iphone - 当 UITextField 被触摸出 ScrollView 的框架时,如何阻止 UIScrollView 自动滚动?

ios - 当 UIView 高度改变时更新 AutoLayout 约束

objective-c - locationOfTouch 和 numberOfTouches

ios: UIScrollView scrollRectToVisible 不工作

ios - UICollectionViewCell 响应多个手势识别器

ios - 如何在 iOS 中制作流畅的布局?

ios - 如何在ios swift形式的控件之间添加控件