我的看法如下:
在我的代码中,我想移动这个 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 移动!
约束
Best Answer-推荐答案 strong>
虽然很难从您的约束层次截图中分辨出问题,但我认为问题与 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/
|