Wednesday, June 19, 2024
HomeiOS Developmentios - I used to be attempting to replace the peak constraint...

ios – I used to be attempting to replace the peak constraint of my view throughout an animation. Its not getting up to date why..?


Right here is how i’ve configured the profile view,

-(void)addProfileView
{
    if(!_profileView)
    {
        _profileView = [ProfileView new];
        _profileView.clipsToBounds = YES;
        _profileView.delegate=self;
        _profileView.alpha = 1.0;
        _profileView.opaque = YES;
        _profileView.translatesAutoresizingMaskIntoConstraints = false;
        [self.view addSubview:_profileView];
        NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:_profileView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:160];
        NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:_profileView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
        NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_profileView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:_profileView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:-64];
        [NSLayoutConstraint activateConstraints:@[heightConstraint, widthConstraint, leftConstraint, topConstraint]];
    }
}

Now, why is the profile view’s top not altering after the animation? Ought to I add the constraints to the superview? What is the distinction between including constraints on to the superview and utilizing [NSLayoutConstraint activateConstraints:@[]] ?

-(void)animateProfileViewBasedOnOffset:(BOOL)isTop
{
__block NSLayoutConstraint *heightConstraint;
    
    for (NSLayoutConstraint * constraint in [_profileView constraints]) {
        if (constraint.firstAnchor == NSLayoutAttributeHeight) {
            heightConstraint = constraint;
            break;
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        if (isTop) {
            heightConstraint.constant = 64;
        }
        else {
            heightConstraint.constant = 160;
        }
        [_profileView layoutIfNeeded];
        [self.view layoutIfNeeded];
    }completion:^(BOOL completion)
     {
         NULL;
     }];
}

The peak of the profileView solely modifications after I add its constraint to the superview ([self.view addConstraints:@[]]). Through the animation, I loop by way of all of the constraints of the superview to search out the precise constraint and replace it.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments