Ios 为什么移动父视图不移动子视图?
我的看法如下:Ios 为什么移动父视图不移动子视图?,ios,objective-c,uiview,Ios,Objective C,Uiview,我的看法如下: -(void) keyboardWillShow:(NSNotification *) notification { NSDictionary* keyboardInfo = [notification userInfo]; // Work out where the keyboard will be NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginU
-(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];
}
在我的代码中,我想移动此视图键盘大小:
-(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
是mainUIView
的子视图。但它并没有移动。什么可以迫使sub view留在那里?这可能是因为限制吗?正如您看到的,子视图和一个标签没有随父视图一起移动
约束
问题是您说的是
self.view.frame=
。不能这样做-此视图已固定为窗口的子视图。给self.view
一个自身大小相同的内容视图(固定在所有四个面上),所有其他视图都在其中,到时候,移动内容视图。此外,您对这些视图有约束(自动布局),因此您不能更改内容视图的框架
;要移动它,请更改其约束。虽然很难从约束层次结构屏幕截图中看出问题所在,但我认为问题与self.memberNumberLayout的顶级约束有关。我的建议如下:
1-通过使用容器视图顶部约束中的IBOutlet,使用约束而不是帧来设置视图动画,然后使用常量,例如self.containerConstraint.constant=keyboardSize*-1代码>
2-使用滚动视图解决此类问题。UIScrollView是在键盘出现或消失时向上移动视图的绝佳视图,当您在不同设备上运行应用程序时,它也会非常有用。有关更多信息,请参阅此问题:hi@Bernard,你能发布你的约束布局截图吗?@Basheer\u CAD Yes mate。我也添加了一些限制。可能重复的Hey@matt你能解释一下你的答案吗。如何为内容视图提供相同大小的视图?你是说我在主视图下添加另一个视图并在该视图上添加元素,还是说我是以编程方式进行的?不是以编程方式,不是。我只是说你要提前计划,因为你知道你以后会改变事情。这里的可下载示例:旧的,但可能仍然有效