2016-01-20 17 views
4

如何以最簡單的方式以編程方式更改乘數?以編程方式更改中心X/Y的對齊約束條件的乘數

enter image description here

的SWIFT 2.0

+0

乘法器是隻讀的,對於動畫和運行時約束條件的程序調整,建議按照我所知玩常量。 – meth

+0

您是否嘗試過解決方案http://stackoverflow.com/questions/19593641/can-i-change-multiplier-property-for-nslayoutconstraint? – Bretsko

回答

5

所以對於Y,如果設置了圖像等於爲0至上海華的頂部的恆定的頂部。然後輸入此代碼:

@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

+0

你是最棒的!我試圖用我的頭打破牆壁,你給我頭痛的藥丸:) –

1

我嘗試使用擴展,但它不能正常工作,但改變全球效用函數,它爲我工作:

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 
} 
相關問題