返回到UIViewController,其视图滚动出来

时间:2012-12-10 06:33:37

标签: objective-c ios cocoa-touch

我正在使用iOS 6.我有UIViewControllerUIScrollViewScrollView是登录屏幕,即标签,文本字段和登录按钮。

为了在键盘弹出时滚动到适当的字段,我使用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];
}

工作正常。当我的模态转换到另一个视图控制器时,问题发生,并解雇回到这里。滚动位置SOMETIMES(间歇性地)完全没有空间。

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

1 个答案:

答案 0 :(得分:0)

尝试将scrollView的名称更改为其他名称。我之前遇到过同样的问题。在同一个vc中有2个UIScrollView,在模态视图显示ana被解雇后,其中一个总是放错地方。