Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animateWithDuration not changing the frame of UIView

I am trying to implement a drop down view which will move down when the user press the button and will be risen above again once the user press the same button. I am using following code for this purpose:

[UIView animateWithDuration:0.25f animations:^{
        self.dropdownView.frame =
        CGRectMake(self.dropdownView.frame.origin.x,
                   self.dropdownView.frame.origin.y+42,
                   self.dropdownView.frame.size.width,
                   self.dropdownView.frame.size.height);
    }
                     completion:^(BOOL finished){
        NSLog(@"%@",self.dropdownView);
    }];

Here what I am trying is to move the origin 42 points down to create dropdown sort of animation. In case of moving up, I am using:

[UIView animateWithDuration:0.25f animations:^{
        self.dropdownView.frame =
        CGRectMake(self.dropdownView.frame.origin.x,
                   self.dropdownView.frame.origin.y-42,
                   self.dropdownView.frame.size.width,
                   self.dropdownView.frame.size.height);
    }
                     completion:^(BOOL finished){
    NSLog(@"%@",self.dropdownView);

    }];

However, if I check the log, the frame of the dropdown view is not updated. However, If I use the same code in another VC with different UI elements, this code is working perfectly fine. In both cases, I have created the dropdown view in storyboard. What is wrong with the code so that it isn't working?

Edit

When I logged the constraints before first animation, the log was:

Constraints: (
"<NSIBPrototypingLayoutConstraint:0x7fcb01797460 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIImageView:0x7fcb01481970](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0ac0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIImageView:0x7fcb01481970]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0b10 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x7fcb01481970(320)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa360 'IB auto generated at build time for view with fixed frame' V:[UIImageView:0x7fcb01481970(42)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa3b0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIButton:0x7fcb017e8080](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017824e0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb017e8080]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01782530 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb017e8080(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fe70 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb017e8080(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fec0 'IB auto generated at build time for view with fixed frame' H:|-(82)-[UIButton:0x7fcb01796ea0](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01776490 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01796ea0]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017764e0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01796ea0(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017956b0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01796ea0(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795700 'IB auto generated at build time for view with fixed frame' H:|-(164)-[UIButton:0x7fcb01699f60](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795860 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01699f60]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017958b0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01699f60(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef40 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01699f60(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef90 'IB auto generated at build time for view with fixed frame' H:|-(242)-[UIButton:0x7fcb01667610](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b44b0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01667610]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b4500 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01667610(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017bbde0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01667610(36)]>")

and the log of constraints once the animation was completed was:

Constraints: (
"<NSIBPrototypingLayoutConstraint:0x7fcb01797460 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIImageView:0x7fcb01481970](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0ac0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIImageView:0x7fcb01481970]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0b10 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x7fcb01481970(320)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa360 'IB auto generated at build time for view with fixed frame' V:[UIImageView:0x7fcb01481970(42)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa3b0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIButton:0x7fcb017e8080](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017824e0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb017e8080]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01782530 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb017e8080(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fe70 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb017e8080(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fec0 'IB auto generated at build time for view with fixed frame' H:|-(82)-[UIButton:0x7fcb01796ea0](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01776490 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01796ea0]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017764e0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01796ea0(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017956b0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01796ea0(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795700 'IB auto generated at build time for view with fixed frame' H:|-(164)-[UIButton:0x7fcb01699f60](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795860 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01699f60]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017958b0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01699f60(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef40 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01699f60(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef90 'IB auto generated at build time for view with fixed frame' H:|-(242)-[UIButton:0x7fcb01667610](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b44b0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01667610]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b4500 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01667610(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017bbde0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01667610(36)]>")

Edit 2

That is what I want to achieve: This is the view before animation. Once I click the arrow button, the view should move down as shown This is the view before animation. Once I click the arrow button, the view should move down as shown

Here, the Arrange meeting and Post concert are two buttons which are subview of my dropdownView. They are there, even before the animation but they are behind the brown view, so they were not being shown, once I pressed the arrow button, they moved down and become visible

Here, the Arrange meeting and Post concert are two buttons which are subview of my dropdownView. They are there, even before the animation but they are behind the brown view, so they were not being shown, once I pressed the arrow button, they moved down and become visible

like image 583
Muhammad Jahanzaib Avatar asked Dec 25 '22 02:12

Muhammad Jahanzaib


1 Answers

When you are using AutoLayout you cannot update the frame or center of an app directly.

It just won't work. The constraints defining the frame will override any change you make and it will stay exactly where it is.

For your animation you need to set up constraints something like this...

enter image description here

You might want to set left space and right space instead of fixed width etc...

The top constraint needs to then be stored in a property...

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

It is this constraint that we can then change to animate the frame of the view. The only part of NSLayoutConstraint that is writable is the constant property. (Ironic)

Anyway, your animation code will be something like this...

self.topConstraint.constant += 42;

[UIView animateWithDuration:0.25f
                 animations:^{
                     [self.view setNeedsLayout];
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"%@",self.dropdownView);
                 }];
like image 69
Fogmeister Avatar answered Dec 28 '22 08:12

Fogmeister