ios - 在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题
<p><p>我有一个 <code>UIScrollView</code>,其中包含 <code>UITextFields</code> 和 <code>UITextViews</code>。我已经注册了 <code>UIKeyboardDidChangeFrameNotification</code>。当我点击文本字段或 TextView 时,会触发确实更改框架通知操作,我调整 ScrollView 的 <code>contentOffset</code>,如下所示</p>
<pre><code>- (void)keyboardDidChangeFrame:(NSNotification *)notification
{
CGRect keyboardEndFrame;
[[ objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
CGRect screenRect = [ bounds];
CGRect intersection;
UIView *theFirstResponder = [.keyWindow findFirstResponder];
if (]) {
if (.statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {
keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 416, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
}
else if (.statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 0, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
}
}
else
keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, keyboardEndFrame.origin.x, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
screenRect = CGRectMake(screenRect.origin.y, screenRect.origin.x, screenRect.size.height, screenRect.size.width);
if(CGRectEqualToRect(lastKBDRect, keyboardEndFrame)) {
return;
}
lastKBDRect = keyboardEndFrame;
if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) {
// Keyboard is visible
//Convert Frame of the first responder, in context of the view that needs to be shifted
UIView *firstResponder = [.keyWindow findFirstResponder];
CGRect theRect = .keyWindow];
theRect = CGRectMake(theRect.origin.y, theRect.origin.x > 768 ? 750 : theRect.origin.x, theRect.size.height, theRect.size.width);
intersection = CGRectIntersection(keyboardEndFrame, theRect);
//If intersection is null, then no need to shift anything. Simply return.
if(CGRectIsNull(intersection)) {
return;
}
//Shift the view so that the first responder view is completely visible, keeping the constraint that the origin of the first responder view is also visible.
//Remember the current offset, so when we shift the view back, we shift it to the proper position.
if (!wasContentViewShifted) {
lastContentOffset = contentScrollView.contentOffset;
lastContentSize = contentScrollView.contentSize;
wasContentViewShifted = YES;
}
CGFloat offset = theRect.origin.y + theRect.size.height - keyboardEndFrame.origin.y;
if((theRect.origin.y - offset) < 40) {
offset += 42;
}
[UIView animateWithDuration:0.3f animations:^{
contentScrollView.contentOffset = CGPointMake(0, contentScrollView.contentOffset.y + offset);
contentScrollView.contentSize = CGSizeMake(0, lastContentSize.height + (600 - theRect.size.height));
}];
} else {
// Keyboard is hidden. Move the view back only if it was shifted.
if(wasContentViewShifted) {
wasContentViewShifted = NO;
[UIView animateWithDuration:0.3f animations:^{
contentScrollView.contentOffset = lastContentOffset;
contentScrollView.contentSize = lastContentSize;
}];
}
}
}
</code></pre>
<p>应用程序仅支持横向。
我在这里面临的问题是</p>
<ol>
<li>点击 textView 会显示键盘,如果 textview 被键盘隐藏,则会滚动到顶部。现在,改变方向(横向从左到横向)使 textView 滚动到顶部,因此不可见。</li>
<li>TextView 的滚动有时适用于横向左侧,但不适用于横向右侧,反之亦然。这是因为我使用了 <code>keyboardEndFrame</code> 值。我不应该使用 <code>keyboardEndFrame</code> 原始值吗?如果没有,还有什么替代方案?</li>
<li>有时它适用于 textField 但不适用于 textView。</li>
</ol></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以尝试使用 <a href="https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present" rel="noreferrer noopener nofollow">this</a>,而不是使用如此复杂的解决方案.
还可以找到一种更简单的解决方案 <a href="https://stackoverflow.com/questions/1247113/iphone-keyboard-covers-text-field" rel="noreferrer noopener nofollow">here</a> .</p>
<p>此外,不建议像您那样对帧进行硬编码。引用 <a href="http://developer.apple.com/iphone/library/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html" rel="noreferrer noopener nofollow">apple documentation</a>了解更多详情。</p></p>
<p style="font-size: 20px;">关于ios - 在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15757527/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15757527/
</a>
</p>
页:
[1]