为什么移动父视图不移动子视图?

时间:2016-04-04 01:24:27

标签: ios objective-c uiview

我的观点如下:

enter image description here

在我的代码中,我想移动此视图键盘大小:

-(void) keyboardWillShow:(NSNotification *) 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];
}

除了儿童观点外,它正在将所有内容移至顶部。然后我尝试将子视图移动到顶部,如下所示:

-(void) keyboardWillShow:(NSNotification *) 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的子视图。但它没有动。什么可以迫使子视图留在那里?可能是因为约束?正如您可以看到子视图,并且一个标签没有随父视图移动!

enter image description here

约束

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

问题是你在说self.view.frame =。你不能这样做 - 这个视图被固定为窗口的子视图。为self.view一个内容视图提供相同的大小(固定到所有四个方面),其中包含所有其他视图,并且当时间到来时,移动内容视图。此外,您对这些视图(自动布局)有约束,因此您不能更改内容视图的frame;移动它,更改其约束

答案 1 :(得分:1)

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

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

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