键盘隐藏我的textview

时间:2010-12-30 05:09:25

标签: iphone uiview keyboard uiscrollview uitextview

我有一个简单的应用程序,它包含2个textview,1个uiview作为coretext子类,然后是1个scrollview。其他部分是来自scrollview的子视图。我使用这个scrollview,因为我需要同时滚动textviews和uiview。我已将它们全部滚动到一起,但问题是,键盘隐藏了textview中的一些行。当键盘出现时我必须改变scrollview的框架,但它仍然无济于事。 这是我的代码:

UIScrollView *scrollView;
UIView *viewTextView;
UITextView *lineNumberTextView;
UITextView *codeTextView;


-(void) viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(keyboardWillAppear:) 
 name:UIKeyboardWillShowNotification 
 object:nil];
[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(keyboardWillDisappear:) 
 name:UIKeyboardWillHideNotification 
 object:nil];

self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width, 
                                           self.codeTextView.frame.size.height);

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, viewTextView.frame.size.height);

[scrollView addSubview:viewTextView];

CGAffineTransform translationCoreText = CGAffineTransformMakeTranslation(60, 7);
[viewTextView setTransform:translationCoreText];

[scrollView addSubview:lineNumberTextView];
[self.scrollView setScrollEnabled:YES];
[self.codeTextView setScrollEnabled:NO];

}

-(void)keyboardWillAppear:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[[notification userInfo] 
                               objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
CGRect keyboardEndingUncorrectedFrame = [[[notification userInfo] 
                                          objectForKey:UIKeyboardFrameEndUserInfoKey ] CGRectValue];
CGRect keyboardEndingFrame = 
[self.view convertRect:keyboardEndingUncorrectedFrame 
              fromView:nil];


self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width, 
                                           self.codeTextView.frame.size.height - keyboardEndingFrame.size.height);

[UIView commitAnimations];

}

-(void)keyboardWillDisappear:(NSNotification *) notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[[notification userInfo] 
                               objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
CGRect keyboardEndingUncorrectedFrame = [[[notification userInfo] 
                                          objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardEndingFrame = 
[self.view convertRect:keyboardEndingUncorrectedFrame 
              fromView:nil];


self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width, 
                                           self.codeTextView.frame.size.height + keyboardEndingFrame.size.height);

[UIView commitAnimations];

}

有人可以帮帮我吗?

更新 这是这个问题的图片:

alt text

我执行代码后,键盘上仍隐藏着一些文字

再次更新 我认为键盘仍然隐藏文本,因为我将textview scroll设置为NO。那是对的吗?

我已将此代码添加到keyboardwillappear方法

codeBuilderSelectedRange = self.codeTextView.selectedRange; [self.viewTextViewScroll setContentOffset:CGPointMake(0, (CGFloat)codeBuilderSelectedRange.location) animated:YES];

但它只是让文本视图从视图中消失...有人能说出答案吗?

5 个答案:

答案 0 :(得分:1)

#pragma mark -------------------------
#pragma mark TextView delegate Methods
- (void)textViewDidBeginEditing:(UITextView *)textView {

    UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];

    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 60, 32)];

    [infoButton setBackgroundImage:[UIImage imageNamed: @"back-btn.png"] forState:UIControlStateNormal];

    [infoButton setTitle:@"Done" forState:UIControlStateNormal];

    infoButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];

    [infoButton addTarget:self action:@selector(resignTextView) forControlEvents:UIControlEventTouchUpInside];

    [parentView addSubview:infoButton];

    [infoButton release];

    UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];

    [parentView release];

    self.navigationItem.rightBarButtonItem = customBarButtomItem;

    [customBarButtomItem release];

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];
    [UIView commitAnimations];

    CGRect frame = self.scrollView.frame;


    self.scrollView.frame.size.height = 206;

    //[self.view setContentOffset:CGPointMake(0,320)];
}



- (void)resignTextView {

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];

    [UIView commitAnimations];

    CGRect frame = self.scrollView.frame;


    self.scrollView.frame.size.height = 460;

    [messageTextView resignFirstResponder];


    self.navigationItem.rightBarButtonItem = nil;
}

答案 1 :(得分:1)

以下是我使用的一些代码。基本上,我们要做的是在UITextField获得焦点时为视图的位置设置动画。为此,我们必须让UIViewController成为UITextField s

的代表

您需要实施的第一个委托方法是- (void)textFieldDidBeginEditing:(UITextField *)textField

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds 
                                                fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds
                                           fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 
            * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y 
            - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
            * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
    }
}

现在,当选择UITextField时,视图已被“推高”,我们希望确保在完成后将其向下滑动:

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

This was taken来自我最喜欢的Cocoa博客Cocoa With Love。 Cheezy的名字,但Matt在Objective-C开发方面有一些很棒的帖子。

答案 2 :(得分:1)

答案 3 :(得分:1)

特别适用于UITextView(不是UITextField)

您可以查看以下教程:http://codingcluster.blogspot.in/2012/03/iphone-make-uitextview-move-up-when.html

我已经实现了它并且按预期工作(至少对我而言)

答案 4 :(得分:1)

这是我搜索过的最佳解决方案并尝试了很多代码,但这个链接非常适合在键盘之外移动textview。 http://codingcluster.blogspot.in/2012/03/iphone-make-uitextview-move-up-when.html