子视图不与其父视图一起移动

Subview not moving along with its superview

我以前做过一百万次,但现在发现自己很困惑。 当您为容器视图设置动画以更改其位置时,它的子视图会随之移动,不是吗?您不会更改子视图的框架,您只需更改父视图的框架,因为所有子视图在其父视图中的位置都不会改变。

但是由于某些原因,这次子视图不会随着它们的父视图一起移动,它会停留在原来的位置。

- (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) {

                         }
                     }
     ];

}

删除 self.writeCommentTextView.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) {

                         }
                     }
     ];
}

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