0

I have an UIImageView which I placed in the center of screen using Align Center X and Align Center Y constraints.I want the image to move with animation to the top of screen keep the horizontal alignment in container and have a space of 20 from the top when i press a button. Remove the Align Center Y constraint programmatically and add the top space constraint is a good approach for that?

1 Answers1

0

Calculate dynamically the constant with the UIScreen class and your view height.

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *centerYConstraint;
@property (nonatomic, weak) IBOutlet UIView *myView;

- (IBAction)moveView:(id)sender;

@end


- (IBAction)moveView:(id)sender
{
     self.centerYConstraint.constant = 20.0 + (self.myView.frame.size.height * 0.5) - ([UIScreen mainScreen].bounds.size.height * 0.5);
    [UIView animateWithDuration:1.0 animations:^{
        [self.view layoutIfNeeded];
    }];
}

You can download the code here (http://cl.ly/3l1B0k1L0h1C)

torcelly
  • 516
  • 4
  • 8