OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么移动父 View 不移动 subview ? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:18
标题: ios - 为什么移动父 View 不移动 subview ?

我的看法如下:

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4