UIScrollView和UIKeyboard的问题

时间:2012-06-14 17:29:05

标签: objective-c uiscrollview uitextview

我在这里遇到了一些问题。当我点击内部时,我有一个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而不是上升时,它会下降......

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

UITextView 已经滚动视图。当您输入的文本多于可见文本时,它应自动滚动。此外,用户可以随时自己滚动文本。

答案 1 :(得分: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); 

    }];
}