1

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 enter image description here

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.

enter image description here enter image description here

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 ?

Tarang
  • 626
  • 2
  • 8
  • 22
  • 1
    You should show the code for how you handle the shrink. If you're actually modifying the frame of the holder view, that's wrong -- you should be doing that with constraints. What constraints do you have between the superview and holder view? – rdelmar Apr 14 '14 at 15:23
  • Actually you are right i was updating the frame of holder view also. But the constraints should take care of it. It works as expected now. Thanks – Tarang Apr 14 '14 at 15:58

2 Answers2

2

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

Community
  • 1
  • 1
BangOperator
  • 4,377
  • 2
  • 24
  • 38
0

You can also use the function updateConstraints.

[settingsView updateConstraints];
Joshua Wolff
  • 2,687
  • 1
  • 25
  • 42