• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么移动父 View 不移动 subview ?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:18:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的看法如下:

enter image description here

在我的代码中,我想移动这个 View 键盘大小:

-(void) keyboardWillShowNSNotification *) notification {
    NSDictionary* keyboardInfo = [notification userInfo];

    // Work out where the keyboard will be
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

    // Work out animation duration
    NSTimeInterval animationDuration =[[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[keyboardInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];


    // Animate this
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:keyboardAnimationCurve
                     animations:^(){
                         self.view.frame = CGRectOffset(self.view.frame, 0, -keyboardFrameBeginRect.size.height);
                     }
                     completion:NULL];
}

它将所有内容都移到顶部,除了 subview 。然后我尝试将 subview 移动到顶部,如下所示:

-(void) keyboardWillShowNSNotification *) notification {
    NSDictionary* keyboardInfo = [notification userInfo];

    // Work out where the keyboard will be
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

    // Work out animation duration
    NSTimeInterval animationDuration =[[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[keyboardInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];


    // Animate this
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:keyboardAnimationCurve
                     animations:^(){
                         self.view.frame = CGRectOffset(self.view.frame, 0, -keyboardFrameBeginRect.size.height);
                         self.memberNumberLayout.frame = CGRectOffset(self.memberNumberLayout.frame, 0, -keyboardFrameBeginRect.size.height);
                     }
                     completion:NULL];
}

self.memberNumberLayout 是主UIView 的 subview 。但它不动。什么可以迫使 subview 留在那里?会不会是因为约束?如您所见, subview 和一个标签没有随其父 View 移动!

enter image description here

约束

enter image description here

enter image description here



Best Answer-推荐答案


虽然很难从您的约束层次截图中分辨出问题,但我认为问题与 self.memberNumberLayout 的顶级约束有关,我的建议如下:

1- 使用约束而不是框架通过使用容器 View 顶部约束中的 IBOutlet 为 View 设置动画,然后使用常量,例如 self.containerConstraint.constant = keyboardSize * -1;

2- 使用 ScrollView 来解决此类问题。 UIScrollView 是一个很好的 View ,可以在键盘出现或消失时向上移动 View ,这在您在不同设备上运行应用程序时也非常有用。有关详细信息,请参阅此问题:How to make a UITextField move up when keyboard is present?

关于ios - 为什么移动父 View 不移动 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393304/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap