【问题标题】:Programmatically Change multiplier of Alignment Constraint of Center X / Y以编程方式更改中心 X / Y 的对齐约束的乘数
【发布时间】:2016-01-20 22:44:19
【问题描述】:

如何以最简单的方式以编程方式更改乘数?

对于 Swift 2.0

【问题讨论】:

标签: ios swift constraints


【解决方案1】:

所以对于 Y,如果您将图像的顶部设置为等于常量 0 到 superView 的顶部。然后输入此代码:

@IBOutlet weak var topc: NSLayoutConstraint!


let heightOfSuperview = self.view.bounds.height
topc.constant = heightOfSuperview * 0.90 // this has the same effect as multiplier  

这相当于 Center.y 乘数 = 0.9:1

【讨论】:

  • 你是最棒的!我正试图用我的头打破墙壁,你给了我头痛的药丸:)
  • 除非这不起作用,因为它是 constant,正如访问者所建议的那样。如果您调整视图大小,则此测量值不会像 multiplier 那样按比例调整。
【解决方案2】:

我尝试使用扩展,但它不起作用,但更改为全局实用功能,它对我有用:

public class func changeMultiplier(constraint: NSLayoutConstraint, multiplier: CGFloat) -> NSLayoutConstraint {
    let newConstraint = NSLayoutConstraint(
        item: constraint.firstItem,
        attribute: constraint.firstAttribute,
        relatedBy: constraint.relation,
        toItem: constraint.secondItem,
        attribute: constraint.secondAttribute,
        multiplier: multiplier,
        constant: constraint.constant)

    newConstraint.priority = constraint.priority
    NSLayoutConstraint.deactivateConstraints([constraint])
    NSLayoutConstraint.activateConstraints([newConstraint])
    return newConstraint
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多