ios工作札记2——UIView层次管理(sendSubviewToBack,bringSubviewToFront)

来源:互联网 发布:sql删除有约束字段 编辑:程序博客网 时间:2024/05/19 21:15

ios

http://segmentfault.com/a/1190000000517494

本文仅作为个人学习总结记录使用!能力有限,难免会有疏漏和错误,还望指出。共同进步。

独白

一个小小的动画,照成的视图覆盖问题,困扰了我一上午。原因还是对UIView的层次管理了解的不够全面.

正文

请输入图片描述
我想在编辑"其他原因"时,会自动弹出键盘,然后开启一个动画,让整个VIEW上移,不让键盘挡住view
PS:navigationBar被我隐藏了,图片上的title是在整个VIEW中的

动画代码如下:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{//    [self.view sendSubviewToBack:self.view_content];    [UIView animateWithDuration:0.35f animations:^{        [self.view_content setFrame:CGRectMake(self.view_content.frame.origin.x, self.view_content.frame.origin.y - 216.0f, self.view_content.frame.size.width, self.view_content.frame.size.height)];    } completion:^(BOOL finished) {    }];    return YES;}

然后结果会变成这样:

自定义的"导航栏"不见了

请输入图片描述

将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法。将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。

修改之后的代码:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{    [self.view sendSubviewToBack:self.view_content];    [UIView animateWithDuration:0.35f animations:^{        [self.view_content setFrame:CGRectMake(self.view_content.frame.origin.x, self.view_content.frame.origin.y - 216.0f, self.view_content.frame.size.width, self.view_content.frame.size.height)];    } completion:^(BOOL finished) {    }];    return YES;}

最终的效果:
请输入图片描述

  • ios
  • 举报

0 0