Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation trying to change the frame size doesn't work in iOS 8 but in iOS 7

I wanted to resize an UIView, my code works perfect in iOS 7.1 but when I run it in iOS 8 it didn't work properly (I will explain below).

I have my UIView int the storyboard with values (0,67,511,320), I want to resize to full screen on iPad, so I added the following code:

        [UIView animateWithDuration:0.5 animations:^{

            CGRect frame = containerView.frame;
            frame.origin.x = 0;
            frame.origin.y = 67;
            frame.size.height = 643;
            frame.size.width = 1024;
            containerView.frame = frame;
            ...

        }completion:^(BOOL finished) {
            ...
        }];

I wanted something like:

 _________________________________
|           |                     |
|           |                     |
|     A     |                     |
|___________|                     |
|                                 |
|                BACKGROUND       |
|                                 |
|                                 |
|_________________________________|

                |
                V
 _________________________________
|                                 |
|                                 |
|                                 |
|               A                 |
|                                 |
|                                 |
|                                 |
|                                 |
|_________________________________|

But it starts the animation like (0,67,0,0) to (0,67,511,320)

Any clue about what is happening? Or alternative?

like image 772
ƒernando Valle Avatar asked Sep 26 '14 08:09

ƒernando Valle


People also ask

What is UIView in iOS?

An object that manages the content for a rectangular area on the screen. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

How do I view an animation in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.


2 Answers

You should disable pregenerated constraints. The most simple way is to use autoresizing masks instead of constraints for animating view.

containerView.translatesAutoresizingMaskIntoConstraints = YES;

You can put this code to your -viewDidLoad method or just above [UIView animateWithDuration:...

like image 55
LorikMalorik Avatar answered Oct 03 '22 09:10

LorikMalorik


is autolayout enabled? maybe in iOS 8 some additional constrains are adding during build time

try to call [self.view layoutIfNeeded]; in animatin block, after animation code itself

UPDATE

__block CGRect frame = containerView.frame;
frame.origin.x = 0;
frame.origin.y = 67;
frame.size.height = 643;
frame.size.width = 1024;

[UIView animateWithDuration:0.5 animations:^{

        containerView.frame = frame;
        ...

    }completion:^(BOOL finished) {
        ...
    }];
like image 25
Roma Avatar answered Oct 03 '22 09:10

Roma