带键盘的UIScrollview:contentOffset问题

时间:2014-04-11 10:17:22

标签: ios objective-c ios7 uiscrollview autolayout

我想滚动滚动视图的内容,下面是执行该操作的代码。即使我以编程方式更改滚动视图,问题contentOffset也不会更改。

// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    NSTimeInterval animationDuration;
    [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    // If active text field is hidden behind keyboard then scroll it so it's visible.
    CGRect aRect = self.contentView.frame;
    aRect.size.height -= kbSize.height;

    // Change contentInset
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height - TABBAR_HEIGHT, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        // scorll the contentView accoring to which textField is selected.
        CGFloat yvalue = - ((self.scrollView.frame.size.height - ((self.editedRealName.isEditing)? self.editedRealName.frame.origin.y :self.birthdate.frame.origin.y)) - self.scrollView.contentOffset.y - (kbSize.height - TABBAR_HEIGHT)) + 35;
        [scrollView setContentOffset:CGPointMake(0, yvalue) animated:YES];
    } completion:nil];   

    // printing contentOffset at this point prints older value.i.e.(0,0).
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    // Here, when print contentOffset value then it prints modified value.

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //scroll the contentView back to top.
        self.scrollView.ContentOffset = CGPointMake(0, 0);
    } completion:nil];

}

更改contentInset向上滚动内容并显示文本字段。但是我希望它能够滚动10个点以使文本字段和键盘之间有10个点的余量。上面的代码边距为0分。

我使用自动布局。我不知道是否有任何与我的问题有关的事情。

0 个答案:

没有答案