2016-05-11 13 views
0

ich diesen Code haben Einschränkungen zu aktualisieren für eine UITextFilediOS: Update Einschränkungen programmatisch

- (void)updateUIOnePassword { 

    NSLayoutConstraint *fullTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]; 
    NSLayoutConstraint *cutTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f]; 

    if ([self isOnepasswordAvailable]) { 
    self.onepasswordButton.alpha = 1.0f; 
    [self.view removeConstraint:fullTextField]; 
    [self.view addConstraint:cutTextField]; 
    } else { 
    self.onepasswordButton.alpha = 0.0f; 
    [self.view addConstraint:fullTextField]; 
    [self.view updateConstraints]; 
    } 

    [self.view setNeedsUpdateConstraints]; 
    [self.view layoutIfNeeded]; 
} 

wenn isOnepasswordAvailable beim Start TRUE ist, es funktioniert gut, nach, wenn ich OP App löschen und isOnepasswordAvailable FALSCH Werke ist wieder gut, aber wenn ich wieder in isOnepasswordAvailable eintrete, wenn ID TRUE, funktionieren die Beschränkungen nicht mehr gut und ich habe eine Warnung in der Konsole. Wissen Sie warum?

Dank

Antwort

0

Obwohl ich Zwang immer lieber eine IBOutlet hat, haben Sie eine Einschränkung programmatisch erstellt ich glaube, dass es einen guten Grund dafür sein muss :)

Paar von Fragen bemerkte ich,

1> Ich habe beide Constraints überprüft, nur dass diese Constraints in ihren konstanten Werten variieren. Ich glaube, Sie müssen nicht zwei verschiedene Einschränkungen erstellen und diese abhängig von der Bedingung hinzufügen und entfernen. Sieht sehr kompliziert aus.

Sie eine Eigenschaft mit dem Namen haben, können

@property (nonatomic,strong) NSLayoutConstraint *textFieldConstraint; 

und in viewWillAppear

if ([self isOnepasswordAvailable]) { 
     self.textFieldConstraint = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]; 
} 
else { 
    self.textFieldConstraint = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f]; 
} 
[self.view addConstraint:self.textFieldConstraint]; 
[self.view layoutIfNeeded]; 

2> Aktualisieren Sie die Methode updateUIOnePassword als

- (void)updateUIOnePassword { 
    if ([self isOnepasswordAvailable]) { 
    self.onepasswordButton.alpha = 1.0f; 
    self.textFieldConstraint.constant = 0.0f; 
    } else { 
    self.onepasswordButton.alpha = 0.0f; 
    self.textFieldConstraint.constant = -0.60f; 
    } 
    [self.view layoutIfNeeded]; 
} 

Dies sollte den Job :) Blick Kumpel :)

+0

Das ist gut, ändern müssen, wenn ich die gleichen Einschränkungen aktualisieren möchten, aber wenn ich will die Einschränkungen ändern ? also entferne es und füge noch ein anderes hinzu? – CrazyDev

+0

Ich verstehe Buddy :) aber in Ihrer Frage die zwei Einschränkung, die Sie entfernt und Hinzufügen hatte Unterschiede nur auf ihren konstanten Wert :) so speziell für diesen Fall schlug ich Update-Constraint anstatt hinzufügen und entfernen sie :) –

+0

danke @Sandeep – CrazyDev

0
- (void)updateUIOnePassword { 

    NSLayoutConstraint *fullTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]; 
    NSLayoutConstraint *cutTextField = [NSLayoutConstraint constraintWithItem:self.passwordTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.userIdTextField attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-60.0f]; 

    if ([self isOnepasswordAvailable]) { 
    self.onepasswordButton.alpha = 1.0f; 
    [self.view removeConstraint:fullTextField]; //Invalid statement 
    [self.view addConstraint:cutTextField]; 
    } else { 
    self.onepasswordButton.alpha = 0.0f; 
    [self.view addConstraint:fullTextField]; 
    [self.view updateConstraints]; 
    } 

    [self.view setNeedsUpdateConstraints]; 
    [self.view layoutIfNeeded]; 
} 

Sie haben eine ungültige Aussage. - updateUIOnePassword() ruft fullTextField wird nicht als Constrain hinzugefügt und es ist nur eine Zuweisung.

bedeutet, dass Sie versuchen, das Objekt zu entfernen, die in dem Array stellt nicht (Array of Constraints)

nun mein Vorschlag ist, kann man eine Klassenstufe Instanz cutTextField und fullTextField sie fügen nur einmal.

, wenn Sie es nicht brauchen

[fullTextField setActive:NO];

Wenn Sie

[fullTextField setActive:YES]; //if already set to NO 
[fullTextField setConstant:100]; 
Verwandte Themen