[英]Move UIScrollView when keyboard comes into place
[编辑:]问题已经解决。 我没有在UIBuilder
正确链接我的UIBuilder
。 代码很好!
我想在键盘出现时调整scrollview的大小。 我去了开发人员文档并找到了这些信息。
在左侧“管理键盘”。
在文档中,它显示了一些代码来检测键盘的大小,然后调整UIScrollView
大小。 我把一个NSLog
代码中的消息功能- (void)keyboardWasShown:(NSNotification*)aNotification
所以我看到这个函数实际上是被调用,但是当我尝试到NSLog
的kbSize
.height它总是在0值。
为什么苹果为此目的提供的代码不起作用?
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 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 -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
您可能想尝试强烈推荐的“TPKeyboardAvoidingScrollView”,可从以下网址获取: https : //github.com/michaeltyson/TPKeyboardAvoiding
奇迹般有效...
您是否曾为该特定通知添加了观察者? 确保在loadView
方法中执行此操作:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
不要忘记在viewDidUnload
方法上取消注册观察者,如下所示:
[[NSNotificationCenter defaultCenter] removeObserver:self];
如果有效,请告诉我!
一个简单的解决方案是使用单行将扩展UIViewController + Keyboard.swift添加到项目中
setupKeyboardNotifcationListenerForScrollView(scrollView)
当键盘出现时它会自动自动调整大小。 不需要任何子类,只是一个扩展! 它可以在GitHub SingleLineKeyboardResize中使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.