如何以编程方式修改约束的乘数?我已经设置了以下内容:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0]];
我需要修改它:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
答案 0 :(得分:5)
您无法更改乘数,因为它是只读的。您必须存储对约束的引用。然后,当您想要更改乘数时,您将删除约束并添加一个新约束。
您可以在视图控制器实现中包含这样的方法:
$scope.signInClick = function() {
console.log('Login was clicked');
$scope.loading = $ionicLoading.show({
content: 'Logging in',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
var params = {'login': ($scope.user.username), 'password': ($scope.user.password)}
console.log("params... " + JSON.stringify(params));
QB.users.create(params, function(err, user){
if (user) {
console.log("successful user.create... " + JSON.stringify(user));
var jid = user.id + "-23837" + "@chat.quickblox.com";
console.log(user.login + "'s jid is......" + jid);
var chatparams = {'jid': jid, 'password': ($scope.user.password)};
QB.chat.connect(chatparams, function(err, roster) {
console.log("err from qb.chat.connect... " + JSON.stringify(err));
console.log("roster from qb.chat.connect .... " + JSON.stringify(roster));
});
}
else {
console.log(JSON.stringify(err));
if (err.message == "Unprocessable Entity"){
QB.login(params, function(err, user){
if (user) {
console.log("Logged into QB with " + JSON.stringify(user));
var jid = user.id + "-23837" + "@chat.quickblox.com";
console.log(user.login + "'s jid is......" + jid);
var chatparams = {'jid': jid, 'password': ($scope.user.password)};
QB.chat.connect(chatparams, function(err, roster) {
console.log("stringifying the err... " + JSON.stringify(err));
console.log("stringifying the roster... " + JSON.stringify(roster));
});
}
else {
console.log(JSON.stringify(err));
}
});
}
}
});
相关问题:
答案 1 :(得分:3)
正如其他人所说,你必须删除旧的并添加一个新的。如果您不想将其存储为属性,只需设置标识符并搜索以便稍后获取
-(NSLayoutConstraint *)constraintWithIndientifer:(NSString *)identifer InView:(UIView *)view{
NSLayoutConstraint * constraintToFind = nil;
for (NSLayoutConstraint * constraint in view.constraints ) {
if([constraint.identifier isEqualToString:identifer]){
constraintToFind = constraint;
break;
}
}
return constraintToFind;
}
然后
NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:self.yellowView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
constraint.identifier = @"1234";
[self.view addConstraint:constraint];
然后你可以得到这个
NSLayoutConstraint * constraint = [self constraintWithIndientifer:@"1234" InView:self.view];
答案 2 :(得分:3)
对于修改乘数我使用约束方法之间的切换 您必须创建具有不同优先级的约束,否则它将破坏布局。
NSLayoutConstraint *constraint1, *constraint2;
// switch between constraints
constraint1.active = NO;
constraint2.active = YES;
[self.view layoutIfNeeded];
self.constraint1.active = true
self.constraint2.active = false
self.view.layoutIfNeeded()
答案 3 :(得分:1)
使用Layout Anchors以编程方式修改约束的乘数是一种更简单的方法:
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1.0/2.0).isActive = true
以下是它的工作原理: