我正在使用 iOS 6。我有UIViewController
一个UIScrollView
. 这ScrollView
是一个登录屏幕,即用于登录的标签、文本字段和按钮。
为了在键盘弹出时滚动到适当的字段,我使用 Apple 开发人员文档中的以下代码:
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = self.activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[self.activeField.superview setFrame:bkgndRect];
[self.scrollView setContentOffset:CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height) animated:YES];
}
- (void)resetUIEdgeInsets
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[self resetUIEdgeInsets];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self resetUIEdgeInsets];
[self registerForKeyboardNotifications];
}
它工作正常。当我模态到另一个视图控制器并解散回到这里时,就会出现问题。滚动位置有时(间歇性地)完全没有空间。
我在这里做错了什么吗。谢谢