iOS约束未按预期更新

时间:2014-04-14 14:07:25

标签: ios objective-c ios7 autolayout nslayoutconstraint

我有一个带圆圈视图的superview视图和一个包含3个标签作为子视图的holderview,并且以superview为中心,如图所示 enter image description here

我在holderview上添加了3个标签的约束,并且还对superview的holderview添加了约束

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(titleLabel);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[titleLabel]-|"
                                        options:0
                                        metrics:nil
                                          views:viewsDictionary];

[holderView addConstraints:constraints];


viewsDictionary = NSDictionaryOfVariableBindings(setLabel);
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[setLabel]-|"
                                        options: 0
                                        metrics:nil
                                          views:viewsDictionary];

[holderView addConstraints:constraints];


viewsDictionary = NSDictionaryOfVariableBindings(repLabel);
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[repLabel]-|"
                                        options:0
                                        metrics:nil
                                          views:viewsDictionary];

[holderView addConstraints:constraints];

viewsDictionary = NSDictionaryOfVariableBindings(titleLabel, setLabel, repLabel);
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[titleLabel]-0-[setLabel]-0-[repLabel]-|"
                                        options:0
                                        metrics:nil
                                          views:viewsDictionary];

[holderView addConstraints:constraints];


NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_labelView);
    NSArray *constraints =[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_labelView]-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDictionary];
    [self addConstraints:constraints];

应用中有一个功能,即圆圈缩小。我希望holderview及其子程序动态缩小。添加约束适用于holderview但子视图未对齐。

enter image description here enter image description here

要收缩,我会在超视图帧更改时更新holderview的帧大小。

任何人都可以指出错误并引导我找到合适的解决方案吗?

2 个答案:

答案 0 :(得分:1)

使用自动布局和更改框架属性会让事情变得混乱。

为您想要更改或动画的约束创建oultest

__weak IBOutlet UIView *settingsView;
__weak IBOutlet NSLayoutConstraint *settingsBottomConstraint;
__weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint;

更新约束(从不框架!)

settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
[settingsView setNeedsUpdateConstraints];
[settingsView layoutIfNeeded];
isSettingsHidden = YES;

最近我使用autolayout处理了视图动画,你可以在这里找到答案 Auto Layout constraint change does not animate

答案 1 :(得分:0)

您还可以使用功能updateConstraints

[settingsView updateConstraints];