2013-06-26 12 views
19

Ich arbeite an einer ios-anwendung. Ich füge das automatische Layout programmatisch zu 2 Etiketten hinzu.ios auto-layout: programmatisch gesetzt width constraint

Ich muss eine Einschränkung hinzufügen, um sie gleiche Breite zu machen.

Ich weiß, wie die Breite eines Etiketts zu beheben, indem Sie:

constraint = [NSLayoutConstraint 
    constraintWithItem:myLabel 
      attribute:NSLayoutAttributeWidth 
      relatedBy:NSLayoutRelationEqual 
       toItem: nil 
      attribute:NSLayoutAttributeNotAnAttribute 
      multiplier:1.0f 
      constant:200.0f]; 

, dass die Etikettengröße auf einen konstanten beheben würde. Aber ich habe 2 Etiketten und ich möchte, dass sie gleich groß sind, ohne eine Konstante setzen zu müssen.

Antwort

16

Es stellte sich heraus, ich muss nur Folgendes tun:

constraint = [NSLayoutConstraint 
    constraintWithItem:myLabel 
     attribute:NSLayoutAttributeWidth 
     relatedBy:NSLayoutRelationEqual 
      toItem: otherLabel 
     attribute:NSLayoutAttributeWidth 
     multiplier:1.0f 
     constant:0]; 
+2

, warum Sie eine toItem brauchen: otherLabel? –

+0

Die Idee besteht darin, "myLabel" und "otherLabel" gleich breit zu machen. also setze ich eine davon in das "withItem" und die andere in das "otherItem" – Youssef

+2

was ist, wenn ich nur eine width constrains zu myLabel hinzufügen möchte? was würde ich für "toItem" sagen. Ich habe es versucht, aber das tut nicht viel. Danke –

Verwandte Themen