2016-12-03 46 views
2

,例如這樣。在使用錨定屬性設置約束之後,從UIView獲取nslayoutconstraints

pageControl.anchorWithFixedHeight(nil, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, topConstant: 0, leadingConstant: 0, bottomConstant: 0, trailingConstant: 0, heightConstant: 30) 

其中anchorWithFixedHeight就像下面的一個輔助功能:

func anchorWithFixedHeight(_ top: NSLayoutYAxisAnchor? = nil, leading: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, trailing: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leadingConstant: CGFloat = 0, bottomConstant: CGFloat = 0, trailingConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 

    translatesAutoresizingMaskIntoConstraints = false 


    if let top = top { 
     anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant)) 
    } 

    if let leading = leading { 
     anchors.append(leadingAnchor.constraint(equalTo: leading, constant: leadingConstant)) 
    } 

    if let bottom = bottom { 
     anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: bottomConstant)) 
    } 

    if let trailing = trailing { 
     anchors.append(trailingAnchor.constraint(equalTo: trailing, constant: trailingConstant)) 
    } 

    anchors.append(heightAnchor.constraint(equalToConstant: heightConstant)) 

} 

現在,比方說,我希望得到底部錨約束來改變其性質,我怎麼能做到這一點?

預先感謝

編輯:這裏的錨是NSlayoutContraint的陣列。我當前的方法以這樣的方式工作,即我將所有需要的約束添加到此數組中,然後獲取任何約束,例如此數組中的底部錨定約束。

但是,我覺得解決方案不夠雄辯。因此,想知道是否還有其他更好的方法。

回答

2

讓我們先清理你方法的簽名。由於常量沒有相應的錨定器是無用的,我們將它們以元組形式傳遞。我們還應該在函數返回後命名函數,這是約束條件,而不是錨點。

如果您想要通過約束來訪問約束,可以通過返回鍵類型爲NSLayoutAttribute並且其值類型爲NSLayoutConstraint的字典以類型安全的方式執行此操作。因此,我們將調用像這樣的新方法:

let root = UIView() 
let view = UIView() 
root.addSubview(view) 

let constraints = view.helperConstraints(
    leading: (root.leadingAnchor, 20), 
    bottom: (root.bottomAnchor, 10), 
    trailing: (root.trailingAnchor, 20), 
    heightConstant: 30) 

// Activate all the new constraints. 
NSLayoutConstraint.activate(Array(constraints.values)) 

// Change the constant of the bottom constraint. 
constraints[.bottom]!.constant = 40 

的實現是不是從你的不同:

extension UIView { 

    func helperConstraints(
     top: (NSLayoutYAxisAnchor, CGFloat)? = nil, 
     leading: (NSLayoutXAxisAnchor, CGFloat)? = nil, 
     bottom: (NSLayoutYAxisAnchor, CGFloat)? = nil, 
     trailing: (NSLayoutXAxisAnchor, CGFloat)? = nil, 
     heightConstant: CGFloat? = nil) -> [NSLayoutAttribute: NSLayoutConstraint] { 

     translatesAutoresizingMaskIntoConstraints = false 
     var constraints = [NSLayoutAttribute: NSLayoutConstraint]() 

     if let (anchor, constant) = top { 
      constraints[.top] = topAnchor.constraint(equalTo: anchor, constant: constant) 
     } 

     if let (anchor, constant) = leading { 
      constraints[.leading] = leadingAnchor.constraint(equalTo: anchor, constant: constant) 
     } 

     if let (anchor, constant) = bottom { 
      constraints[.bottom] = bottomAnchor.constraint(equalTo: anchor, constant: constant) 
     } 

     if let (anchor, constant) = trailing { 
      constraints[.trailing] = trailingAnchor.constraint(equalTo: anchor, constant: constant) 
     } 

     if let constant = heightConstant { 
      constraints[.height] = heightAnchor.constraint(equalToConstant: constant) 
     } 

     return constraints 
    } 

} 
+0

感謝您的回答。(: – user3608914