自动布局约束带动态变量? [英] Auto Layout Constraint With Dynamic Variable?

查看:106
本文介绍了自动布局约束带动态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有一个自动布局约束( NSLayoutConstraint )与动态常数/乘数?

Is it possible to have an Auto Layout constraint (NSLayoutConstraint) with a dynamic constant/multiplier?

例如,这将是一个沼泽标准 NSLayoutConstraint

For example, this would be a bog standard NSLayoutConstraint:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:obj1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:obj2 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];

然后在这里将是约束的改变,但在不断的动态变量:

Then here would be an alteration of that constraint but a dynamic variable in the constant:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:obj1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:obj2 attribute:NSLayoutAttributeLeft multiplier:1 constant:scrollView.contentOffset.x];

第二个将采取 contentOffset 滚动视图,并以此为常数。不过,在尝试这样做,它仅使用偏移约束时作出它存在。

The second one would take the contentOffset of the scrollview and use it as the constant. However, having tried this, it only uses the offset which exists when the constraint is made.

我希望它有约束更新不断时,如果滚动滚动视图什么。这样,它会继续使用最先进的日期 contentOffset

What I would want it to have the constraint update the constant, when the scrollview if scrolling. This way it would keep using the most up to date contentOffset.

这可能吗?谢谢你。

推荐答案

是肯定。

在事实上,这是他们是为建造。当您设置动画的看法等等,你需要能够改变的制约。

In fact, that is what they are built for. When you are animating views etc... you need to be able to change the constraints.

反直觉 NSLayoutConstraint 的唯一属性,该属性是可写的就是属性。 (笑)

Counter-intuitively the only property of NSLayoutConstraint that is writable is the constant property. (lol)

您是不是在你的code虽然是很正确的。

You are not quite right in your code though.

创建约束,你会先为它创建一个属性...

Creating the constraint you would first create a property for it...

@property (nonatomic, strong) NSLayoutConstraint *leftConstraint;

然后创建它...

Then create it...

self.leftConstraint = [NSLayoutConstraint constraintWithItem:obj1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:obj2 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];

然后编辑现有的约束......

Then edit the already existing constraint...

self.leftConstraint.constant = scrollView.contentOffset.x;

然后,你需要强制视图重新布局的子视图...

Then you need to force the view to relayout the subviews...

[self.view layoutIfNeeded];

这篇关于自动布局约束带动态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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