我有一部 iPhone,我正在添加横向支持。之前只是肖像。
当键盘显示时,我无法将文本字段移开。
我使用的解决方案与记录在案的 Apple 解决方案非常相似 here .
该解决方案在纵向模式下工作正常,但当我进入横向模式时它根本不起作用。 View 不会向上滚动,而是向下滚动。
我认为这可能与键盘的高度/宽度有关,我认为我正在正确地考虑到这一点。
感谢任何指出我的愚蠢或更好的解决方案的建议。
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect activeFieldFrame = activeField.frame;
CGFloat kbHeight;
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
kbHeight = kbSize.height;
}
else
{
kbHeight = kbSize.width;
}
UIEdgeInsets contentInsets;
contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbHeight;
activeFieldFrame.origin.y += scrollView.frame.origin.y+aRect.origin.y;
if (!CGRectContainsRect(aRect, activeFieldFrame) )
{
CGPoint scrollPoint = CGPointMake(0.0, activeFieldFrame.origin.y-kbHeight-aRect.origin.y);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
请注意,我在键盘 WILL 中执行此操作,因为我认为在显示键盘之前移动 View 看起来更好。
最佳答案
好的,我会用我的解决方案回答我自己的问题。当然,我似乎在发布到 SO 后立即找到了解决方案。
我的数学全错了。
这是我的更新版本,以防对其他人有帮助。
这将适用于横向和纵向。
- (void)keyboardWillShow:(NSNotification *)aNotification
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect activeFieldFrame = activeField.frame;
CGRect scrollViewFrame = scrollView.frame;//used so I can see values below while debugging
CGFloat kbHeight;
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
kbHeight = kbSize.height;
}
else
{
kbHeight = kbSize.width;
}
UIEdgeInsets contentInsets;
contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbHeight;
activeFieldFrame.origin.y += scrollViewFrame.origin.y+aRect.origin.y;
if (!CGRectContainsRect(aRect, activeFieldFrame) )
{
//add +5 here to give just a little room between field and keyboard
//subtracting scrollviewFrame.origin.y handles cases where scrollview is not at top (0,0) of the view
CGPoint scrollPoint = CGPointMake(0.0, activeFieldFrame.origin.y + activeFieldFrame.size.height + 5 - aRect.size.height - scrollViewFrame.origin.y);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
}
我遇到的一件有趣的事情是,如果您注册以在 View A 上获取键盘通知并在 View A 顶部弹出模态视图 B,并且模态视图 B 也需要键盘恶作剧,因此您将模态视图 B 注册到处理它们, View A 仍将获得键盘事件并执行代码,即使它在屏幕上不可见!
原始 View A 仍将获得键盘将显示事件(如果它是基于选项卡的设计的一部分),而另一个 View C 具有由另一个选项卡显示的编辑字段。
当您的 View 消失时,您应该取消注册监听键盘事件,而当您的 View 出现时注册事件,这似乎是合乎逻辑的。
希望这对其他人有帮助。
关于iOS 横向 View 在键盘显示时移动文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481386/