0

我这里有一些问题。我有一个 UITextView,当我点击里面时,它会被键盘关闭,所以,我在这里要做的是一个 UIScrollView,它让我可以看到我在 TextView 中写的内容......

到目前为止,这是我的代码:

-(void)keyboardWillShow {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView animateWithDuration:.3 animations:^{
        CGRect rect = self.view.frame;
        if (movedUp)
        {
            scrollView.contentSize = CGSizeZero;
            scrollView.frame = CGRectMake(0, 80, 320, 308);
//            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//            rect.size.height += kOFFSET_FOR_KEYBOARD;
        }
        else
        {
            scrollView.contentSize = CGSizeZero;
            scrollView.frame = CGRectMake(0, 190, 320, 308);
//            rect.origin.y += kOFFSET_FOR_KEYBOARD;
//            rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
        self.view.frame = rect;
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}

问题是代码不能正常工作,当我点击 textView 视图而不是上升时,它下降了......

有任何想法吗?

4

2 回答 2

0

我得到了所有问题的答案!

- (void)textViewDidBeginEditing:(UITextView *)textView{

    [self scrollViewAsd:130];

}

- (void)textViewDidEndEditing:(UITextView *)textView{

    [self scrollViewAsd:0];

}

- (void)scrollViewAsd:(float) inset
{

    [UIView animateWithDuration:.3 animations:^{
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, inset, 0);
        scrollView.contentOffset = CGPointMake(0, inset); 

    }];
}
于 2012-06-15T16:02:53.567 回答
0

已经UITextView是一个滚动视图。当您输入的文本多于可见时,它应该会自动滚动。此外,用户可以随时自行滚动里面的文字。

于 2012-06-15T08:27:59.973 回答