[编辑:]问题已经解决。我没有在 UIBuilder
中正确链接我的委托(delegate)。代码很好!
我正在尝试在键盘出现时调整 ScrollView 的大小。我去了开发者文档并找到了这些信息。
在左侧“管理键盘”。
在文档中,它显示了一些代码来检测键盘的大小,然后调整 UIScrollView
的大小。我在函数 - (void)keyboardWasShown:(NSNotification*)aNotification
的代码中放置了一条 NSLog
消息,所以我看到该函数实际上正在被调用,但是当我尝试将 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
像魅力一样工作......
关于iphone - 键盘到位时移动 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590636/