0

我正在使用 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];
}

它工作正常。当我模态到另一个视图控制器并解散回到这里时,就会出现问题。滚动位置有时(间歇性地)完全没有空间。

我在这里做错了什么吗。谢谢

4

1 回答 1

0

尝试将您的滚动视图的名称更改为其他名称。我以前也有同样的问题。在同一个 vc 中有 2 个 UIScrollView,在模态视图呈现后,其中一个总是放错位置。

于 2012-12-10T07:37:19.373 回答