iosobjective-cuiviewanimationsubviewsuperview

Subview not moving along with its superview


I've done this a million times before but finding myself confused now. When you animate a container view to change its position, its subviews move along with it don't they? You don't change subviews' frames you just change superview's frame because all subviews' positions inside their superview don't change.

But for some reason this time the subviews would not move along with their superview, it'd stay stuck at its original position.

- (void)writeCommentTapped{

    UIView *writeCommentView = [[UIView alloc]init];
    writeCommentView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview: writeCommentView];
    [self.view bringSubviewToFront:self.navView];

    self.writeCommentTextView = [[UITextView alloc]init];
    self.writeCommentTextView.backgroundColor = [UIColor blackColor];
    [writeCommentView addSubview:self.writeCommentTextView];
    [self.writeCommentTextView becomeFirstResponder];
    self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;

    writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
    self.writeCommentTextView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
    self.writeCommentTextView.center = writeCommentView.center;


    [UIView animateWithDuration:0.15
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{

                         writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);

                     } completion:^(BOOL finished){
                         if (finished) {

                         }
                     }
     ];

}

Solution

  • Remove self.writeCommentTextView.center and set calculated frame for center

    -(void)writeCommentTapped{
    
    
        UIView *writeCommentView = [[UIView alloc]init];
        writeCommentView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview: writeCommentView];
        [self.view bringSubviewToFront:self.navView];
    
        self.writeCommentTextView = [[UITextView alloc]init];
        self.writeCommentTextView.backgroundColor = [UIColor blackColor];
        [writeCommentView addSubview:self.writeCommentTextView];
        [self.writeCommentTextView becomeFirstResponder];
        self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
    
        writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
        writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
    
    
        self.writeCommentTextView.frame = CGRectMake(40/2, 100/2, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
    //    self.writeCommentTextView.center = writeCommentView.center;
    
    
        [UIView animateWithDuration:0.15
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
    
                             writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
    
                         } completion:^(BOOL finished){
                             if (finished) {
    
                             }
                         }
         ];
    }
    

    OR

    self.writeCommentTextView.center = CGPointMake(writeCommentView.frame.size.width/2.0, writeCommentView.frame.size.height/2.0);