所以我想摆脱代码中的所有“框架”,完全切换到自动布局。但是在一种情况下,我希望 View 的 Y 位置依赖于 content offset
。 ( bounds.origin.y
) 属于 UIScrollView
.使用 auto layout
执行此操作的正确方法是什么? ?一个想法是有一个常量 NSLayoutConstraint
每次更新 scrollview content offset
被改变了,(但是在这里使用自动布局有什么意义呢?)所以我希望有更好的方法来实现“纯”autolayout
.
我知道这里经常讨论“Autolayout
& UIScrollView
”,所以我想强调我不想为 UIScrollView
subview 使用自动布局,我有一个 UIView
有 2 个 subview ;一个 UIScrollView
和一个 UIView
对象,我想使用自动布局来指定 UIView
顶部坐标和 UIScrollView
内容偏移量之间的关系。
最佳答案
好的,您可以很容易地做到这一点。本质上就像您使用框架一样。
首先,您需要创建“可变”约束并将其保存到属性中。
你可以在代码中做到这一点......
self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.theView
attribute:
relatedBy:
toItem:
attribute:
multiplier:
constant:];
// set up the rest
[self.view addConstraint:self.heightConstraint];
或者在 Interface Builder 中创建约束,然后按住 Ctrl 键拖动到属性。 (与任何其他 IBOutlet 一样)。
然后当scrollView滚动时...
self.heightConstraint.constant = calculatedValueFromTheOffset;
[self.view layoutIfNeeded];
这将移动 View ,就像您使用框架一样,但使用自动布局约束。
关于ios - 自动布局和 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21381159/