ios - 为什么移动父 View 不移动 subview ?
<p><p>我的看法如下:</p>
<p> <a href="/image/kEdaE.png" rel="noreferrer noopener nofollow"><img src="/image/kEdaE.png" alt="enter image description here"/></a> </p>
<p>在我的代码中,我想移动这个 View 键盘大小:</p>
<pre><code>-(void) keyboardWillShow:(NSNotification *) notification {
NSDictionary* keyboardInfo = ;
// Work out where the keyboard will be
NSValue* keyboardFrameBegin = ;
CGRect keyboardFrameBeginRect = ;
// Work out animation duration
NSTimeInterval animationDuration =[ doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [ 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];
}
</code></pre>
<p>它将所有内容都移到顶部,除了 subview 。然后我尝试将 subview 移动到顶部,如下所示:</p>
<pre><code>-(void) keyboardWillShow:(NSNotification *) notification {
NSDictionary* keyboardInfo = ;
// Work out where the keyboard will be
NSValue* keyboardFrameBegin = ;
CGRect keyboardFrameBeginRect = ;
// Work out animation duration
NSTimeInterval animationDuration =[ doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [ 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];
}
</code></pre>
<p><code>self.memberNumberLayout</code> 是主<code>UIView</code> 的 subview 。但它不动。什么可以迫使 subview 留在那里?会不会是因为约束?如您所见, subview 和一个标签没有随其父 View 移动!</p>
<p> <a href="/image/igCwl.png" rel="noreferrer noopener nofollow"><img src="/image/igCwl.png" alt="enter image description here"/></a> </p>
<p><strong>约束</strong></p>
<p> <a href="/image/tJkGC.png" rel="noreferrer noopener nofollow"><img src="/image/tJkGC.png" alt="enter image description here"/></a> </p>
<p> <a href="/image/U8DkG.png" rel="noreferrer noopener nofollow"><img src="/image/U8DkG.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>虽然很难从您的约束层次截图中分辨出问题,但我认为问题与 <code>self.memberNumberLayout</code> 的顶级约束有关,我的建议如下:</p>
<p>1- 使用约束而不是框架通过使用容器 View 顶部约束中的 IBOutlet 为 View 设置动画,然后使用常量,例如 <code>self.containerConstraint.constant = keyboardSize * -1;</code> </p>
<p>2- 使用 ScrollView 来解决此类问题。 UIScrollView 是一个很好的 View ,可以在键盘出现或消失时向上移动 View ,这在您在不同设备上运行应用程序时也非常有用。有关详细信息,请参阅此问题:<a href="https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present" rel="noreferrer noopener nofollow">How to make a UITextField move up when keyboard is present?</a> </p></p>
<p style="font-size: 20px;">关于ios - 为什么移动父 View 不移动 subview ?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36393304/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36393304/
</a>
</p>
页:
[1]