- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我实现了一项功能,当键盘隐藏选定的文本输入时自动 ScrollView (我的和教程中的实际上是一样的)。
不幸的是,有一个不良行为:我的 scrollView 的 contentSize
属性随着键盘的高度而增加。因此,当键盘仍然可见时,我可以 ScrollView ,但在适当的内容下方会出现一个空白区域。这是我想避免的事情。我知道这是由更改 contentInset
属性引起的,所以也许还有另一种方法可以做到这一点而没有副作用。
首先我为 UIKeyboardDidShowNotification
和 UIKeyboardWillHideNotification
注册观察者:
- (void)registerKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
此函数在 viewWillAppear
中调用。方法 keyboardWasShown
和 keyboardWillBeHidden
如下所示:
- (void)keyboardWasShown:(NSNotification *)notification
{
NSDictionary *info = [notification 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;
CGRect rect = self.view.frame;
rect.size.height -= kbSize.height;
if(!CGRectContainsPoint(rect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, 2*activeField.frame.size.height+activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
正如我之前所写,它基本上是 Apple 的解决方案。
最佳答案
Content inset 旨在允许访问可能隐藏在键盘下方的 ScrollView 部分(例如)。因此,如果您的内容底部有一个 TextView ,用户将无法与之交互,因为它会隐藏在键盘窗口下方。使用内容插入(按照 Apple 示例),您可以滚动更多内容并显示 TextView 。它实际上并没有增加 contentSize
属性。
关于iphone - 键盘出现后 UISCcrollView 的 contentSize 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14771002/
基于: https://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPh
我是一名优秀的程序员,十分优秀!