Changing the position of UIView

2019-08-02 12:33发布

问题:

I need to set the position of UIView. The code below shows how I do it. But it does not work. How do I do this?

- (void)viewDidLoad
{
  [super viewDidLoad];

      CGRect frame = myview.frame;
      frame.origin.x = myview.frame.origin.x;
      frame.origin.y =   0;
      myview.frame = frame;

NSLog(@"%d", frame.origin.y );

}

回答1:

UIView's have a center property..if you want to change the position then just use that... rather to resize whole view

myview.center = CGPointMake(50, 250);

Hope it helps you.



回答2:

Make sure that your OUTLET is connected to your view in the nib file.



回答3:

Try to do this in viewDidAppear method,it works for me.



回答4:

Move view/ change the position of view with animation

if you want to change the position of View as you want then just use following lines of code :

.h file :

- (void)moveView:(UIView *)view withDuration:(NSTimeInterval)duration
        andCurve:(int)curve inDirection_X:(CGFloat)x inDirection_Y:(CGFloat)y;

.m file :

- (void)moveView:(UIView *)view withDuration:(NSTimeInterval)duration
        andCurve:(int)curve inDirection_X:(CGFloat)x inDirection_Y:(CGFloat)y
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
    view.transform = transform;

    // Commit the changes
    [UIView commitAnimations];
}

Note : To call above method anywhere just do like it , As per your requirement i.e. how to move view/ change the position of view with animation

 [self moveView:mFrontSideView withDuration:2.0 andCurve:0 inDirection_X: 0.0  inDirection_Y: mBackSideView.center.y + 100.0];