简体   繁体   中英

iOS constraints not updating as expected

I have a superview with circle view and a holderview that contains 3 labels as subview and is centred to the superview as seen in image在此处输入图片说明

I have added constraints to the 3 labels with respect to holderview and also added constraints to holderview with respect to superview

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];

There is a feature in app where the circle shrinks. I want the holderview and its subivews to shrink dynamically. Adding the constraints works for holderview but the subviews get misaligned.

在此处输入图片说明在此处输入图片说明

To shrink i update the frame size of the holderview as the superview frame changes.

Can anyone point out the mistakes and guide me to proper solution ?

Using auto layout and changing frame property messes up things.

Create oultest to the constraints that you want to change or animate

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

Update the constrains(Never the frame!)

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

Recently I have worked with animation of views with autolayout and you can find your answer here Auto Layout constraint change does not animate

您还可以使用函数updateConstraints

[settingsView updateConstraints];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM