【发布时间】:2014-03-13 06:02:07
【问题描述】:
我一直在通过以下网址阅读 Apple 提供的文本编程指南:
我试图防止按钮和文本字段被键盘重叠。
当我按照指南的说明粘贴清单 5-1(如下所列)中的代码时,我收到如下错误:
" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? "
和
" Property 'contentsInset' not found in 'scrollView' "
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (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 app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
【问题讨论】:
-
你真的使用滚动视图吗?
-
是的,我已经将 UIViewController 的内容(两个文本字段和一个按钮)嵌入到滚动视图 @GenieWanted
-
你的 UIScrollView 叫什么名字?您需要指定它来代替 scrollView。
-
我如何检查这个? @GenieWanted
-
您必须在 .h 文件中声明了 UIScrollView。对吗?
标签: ios iphone objective-c uiscrollview uikeyboard