Hello! I have a UIView and I cannot change its position (coordinates). I try this: _playerMapView.frame = CGRectMake(15, 15, 50, 50);

I need this in order to change the location of a UIView with animation. I try it here and it doesn't work:

 [UIView animateWithDuration:5.0 animations:^{ _playerMapView.frame = CGRectMake(15, 15, 50, 50); }]; 

Please help me make a position change with animation

  • Does it work without animation? - Max Mikheyenko
  • most likely _playerMapView = nil, this happens, for example, if a variable is not bound in InterfaceBuilder - Andrew Romanov
  • Everything is tied and no, it does not work without animation. I dug the information that you can't change it in such a way, if you use autolayout. But I never found a way out - Andrey Evstratenko
  • It depends on where you call this code, in which thread, etc. - Tikhonov Alexander

1 answer 1

Try this:

 [UIView animateWithDuration:5.0 animations:^{ CGRect viewNewFrame = _playerMapView.frame; viewNewFrame.origin.y += 100; _playerMapView.frame = viewNewFrame; }]; 

The second option is using autolayout and your view has a specific position on the screen. Then you need to make an IBOutlet for constraint and change the constraint directly. Like that:

 [UIView animateWithDuration:5.0 animations:^{ constraint.constant += 40; }]; 

Here you can see how to do it.