ObjC,以编程方式添加/删除后,恢复为界面生成器自动布局约束吗? [英] ObjC, revert to interface builder autolayout constraints, after adding / removing programmatically?

查看:88
本文介绍了ObjC,以编程方式添加/删除后,恢复为界面生成器自动布局约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序的免费版本,我删除了+添加了自动布局约束(以隐藏高级功能),但是,如果用户购买了我的应用程序,我想恢复为界面构建器设置的约束./p>

我希望有一种方法可以实现这一目标,但到目前为止我还没有找到?

这就是我所拥有的

if (!purchased) {

[self.tblOtherAccounts addConstraints:[NSLayoutConstraint
   constraintsWithVisualFormat:@"V:[tblOtherAccounts(==0)]"
   options:0
   metrics:nil
   views:NSDictionaryOfVariableBindings(tblOtherAccounts)]];


NSDictionary *views = @{ @"tblOtherAccounts" : self.tblOtherAccounts, 
   @"butBackAllAc" : butBackAllAc  };

[self.view removeConstraints:[NSLayoutConstraint 
   constraintsWithVisualFormat:@"V:[tblOtherAccounts]-(12)-[butBackAllAc]"
   options:0
   metrics:nil
   views:views]];

[self.view addConstraints:[NSLayoutConstraint 
   constraintsWithVisualFormat:@"V:[tblOtherAccounts]-(0)-[butBackAllAc]"
   options:0
   metrics:nil
   views:views]];
}

解决方案

添加IBOutlet将约束存储在ViewController中:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;

然后更新其值:

_myConstraint.constant = 100.0f;
[_myView setNeedsUpdateConstraints];
[_myView layoutIfNeeded];


关于将关系设置为等于"的评论,我认为您将无法做到.

您可以做的一件事是,使用情节提要生成的约束作为占位符:

NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:_myConstraint.firstItem
                                                                 attribute:_myConstraint.firstAttribute
                                                                 relatedBy:_myConstraint.relation
                                                                    toItem:_myConstraint.secondItem
                                                                 attribute:_myConstraint.secondAttribute
                                                                multiplier:_myConstraint.multiplier
                                                                  constant:_myConstraint.constant];

[_myConstraint.secondItem removeConstraint:_myConstraint];
[constraint.secondItem addConstraint:newConstraint];

当然,上面的代码将创建与约束完全相同的副本,但是您可以对其进行编辑以更改适当的值以满足您的需求.

I have a free version of my app and I remove + add autolayout constraints (to hide a premium feature), however if the user purchases my app I'd like to revert back to to the constraints set by interface builder.

I'm hoping there's a method which will achieve this, but I haven't been able to find it so far?

Here's what I have

if (!purchased) {

[self.tblOtherAccounts addConstraints:[NSLayoutConstraint
   constraintsWithVisualFormat:@"V:[tblOtherAccounts(==0)]"
   options:0
   metrics:nil
   views:NSDictionaryOfVariableBindings(tblOtherAccounts)]];


NSDictionary *views = @{ @"tblOtherAccounts" : self.tblOtherAccounts, 
   @"butBackAllAc" : butBackAllAc  };

[self.view removeConstraints:[NSLayoutConstraint 
   constraintsWithVisualFormat:@"V:[tblOtherAccounts]-(12)-[butBackAllAc]"
   options:0
   metrics:nil
   views:views]];

[self.view addConstraints:[NSLayoutConstraint 
   constraintsWithVisualFormat:@"V:[tblOtherAccounts]-(0)-[butBackAllAc]"
   options:0
   metrics:nil
   views:views]];
}

解决方案

Add an IBOutlet to store your constraint in your ViewController :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;

Then update its value :

_myConstraint.constant = 100.0f;
[_myView setNeedsUpdateConstraints];
[_myView layoutIfNeeded];


Regarding your comment on setting the relation to "equals", I don't think you'll be able to do that.

One thing you can do though is use your storyboard-generated constraint as a placeholder :

NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:_myConstraint.firstItem
                                                                 attribute:_myConstraint.firstAttribute
                                                                 relatedBy:_myConstraint.relation
                                                                    toItem:_myConstraint.secondItem
                                                                 attribute:_myConstraint.secondAttribute
                                                                multiplier:_myConstraint.multiplier
                                                                  constant:_myConstraint.constant];

[_myConstraint.secondItem removeConstraint:_myConstraint];
[constraint.secondItem addConstraint:newConstraint];

Of course the code above will create an exact duplicate of your constraint, but you can edit it to change the appropriate values to fit your needs.

这篇关于ObjC,以编程方式添加/删除后,恢复为界面生成器自动布局约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆