简体   繁体   English

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

[英]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) {

                         }
                     }
     ];

}

Remove self.writeCommentTextView.center and set calculated frame for center 删除self.writeCommentTextView.center并设置计算框架为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM