【发布时间】:2011-12-15 23:39:38
【问题描述】:
我是 iOS 编程新手,在编辑被键盘遮挡的 UITextField 时,我无法让 UIScrollView 移动。该代码直接来自Apple's documentation,但由于某种原因无法正常工作。
通过调试,我发现通知似乎已正确传递(即它记录“视图应调整大小”,但仅当 activeField 是键盘下方的 textField 时)并且滚动点设置正确,但滚动视图仍然不动。另外,我有理由确定委托模式是正确的(ViewController 是 textFields 和 scrollView 的委托)
- (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];
NSLog(@"%@",@"view should resize");
}
}
由于代码直接来自文档,我可能只是缺少一些简单的东西。谁能指出我要检查的事情的方向?
【问题讨论】:
-
仅供参考:您可以将最后一行更改为
NSLog(@"view should resize"); -
您是否设置了合适的代表?
标签: iphone objective-c animation