gpt4 book ai didi

ios - iOS8中如何修改NSLayoutConstraint的 'mutiplier'属性的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:52 24 4
gpt4 key购买 nike

我使用自适应布局功能来设计应用。我采用了“纵横比”约束的 IBOutlet。我想将此 Aspect Ratio Value 的值更改为当前值的两倍。问题是“constraint”属性可以很容易地从代码中设置,但是“multiplier”属性是只读属性。对于 Aspect Ratio Change,需要更改“multipier”值。我该怎么做?

@property (retain, nonatomic) IBOutlet NSLayoutConstraint *leftImageWidthAspectRatio;

在代码中

NSLog(@"cell.leftImageWidthAspectRatio:%@ : %lf  %lf",cell.leftImageWidthAspectRatio, cell.leftImageWidthAspectRatio.constant,cell.leftImageWidthAspectRatio.multiplier);

结果

 cell.leftImageWidthAspectRatio:<NSLayoutConstraint:0x7c9f2ed0 UIView:0x7c9f2030.width == 2*RIFeedThumbImageView:0x7c9f2c90.width> : 0.000000  2.000000

最佳答案

你是对的——不支持更改现有约束的乘数。 constant 是异常(exception),而不是规则。来自docs :

Unlike the other properties, the constant can be modified after constraint creation. Setting the constant on an existing constraint performs much better than removing the constraint and adding a new one that's exactly like the old except that it has a different constant.

您需要做的就是最后所描述的:将现有约束替换为相同但具有不同乘数的约束。这样的事情应该有效:

NSLayoutConstraint *oldConstraint = cell.leftImageWidthAspectRatio;
CGFloat newMultiplier = 4; // or whatever
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:oldConstraint.firstItem attribute:oldConstraint.firstAttribute relatedBy:oldConstraint.relation toItem:oldConstraint.secondItem attribute:oldConstraint.secondAttribute multiplier:newMultiplier constant:oldConstraint.constant];
newConstraint.priority = oldConstraint.priority;
[cell removeConstraint:oldConstraint];
[cell addConstraint:newConstraint];

请注意,cell 可能是错误的 View ——这取决于 IB 决定放置原始约束的位置。如果这不起作用,请深入了解受约束 View 的超 View (您可以使用 constraints 属性检查它们有哪些约束),直到找到它的终点。

关于ios - iOS8中如何修改NSLayoutConstraint的 'mutiplier'属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com