ios - 自动布局和 UIScrollView

标签 ios uiscrollview uikit autolayout

所以我想摆脱代码中的所有“框架”,完全切换到自动布局。但是在一种情况下,我希望 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/

相关文章:

ios - 阻止 ios : what is this method map: before a block?

ios - 为什么异步网络测试在 Objective-C/Swift 中很困难?

ios - 如何在动态创建的文本字段中设置 ScrollView 的内容偏移量?

ios - 如何在 Swift 中获取 UIScrollView 垂直方向?

ios - 滚动到该区域时,Tiled ScrollView 不显示某些图 block

ios - 如何使用 Swift 在基于 UIKit 的游戏中保存/加载游戏状态

iOS (Swift) - 应用程序委托(delegate)窗口的 Root View Controller 在模拟器中工作,但在手机上加载应用程序时失败

ios - 如何在 iTunes Connect 的实时应用程序中更改 SKU?

ios - Xcode 7.0.1 - 无法识别 SDK > "iPhoneOSX.X (SDK not found)"

iphone - 如何重新创建 UITabBarItem 图像过滤器?