键盘隐藏我的文本视图

发布于 2024-10-09 12:59:26 字数 3365 浏览 0 评论 0原文

我有一个简单的应用程序,它由 2 个文本视图、1 个作为 coretext 子类的 uiview 和 1 个滚动视图组成。其他部分是滚动视图的子视图。我使用这个滚动视图是因为我需要同时滚动文本视图和UIView。我已经将它们全部滚动在一起,但问题是,键盘隐藏了文本视图中的一些行。当键盘出现时,我必须更改滚动视图的框架,但它仍然没有帮助。 这是我的代码:

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

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

更新再次 我认为键盘仍然隐藏文本,因为我将文本视图滚动启用设置为“否”。是吗?

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

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

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

i have a simple app, it consist of 2 textview, 1 uiview as a coretext subclass, and then 1 scrollview. the others part is subviews from scrollview. I use this scrollview because i need to scroll the textviews and uiview at the same time. I already scroll all of them together, but the problem is, the keyboard hiding some lines in the textview. I have to change the frame of scrollview when keyboard appear, but it still not help.
This is my code :

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];

}

can somebody help me please?

UPDATE
this is a pic from this problem :

alt text

some text still hiding by the keyboard after i do my code

UPDATE AGAIN
i think the keyboard still hiding the text because i set the textview scroll enable to be NO. is that right??

i have add this code to the keyboardwillappear method

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

but it just make the textview dissappear from the view...can somebody tell the answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光磨忆 2024-10-16 12:59:26
#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;
}
#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;
}
梦里°也失望 2024-10-16 12:59:26

这是我使用过的一些代码。基本上,我们要做的就是每当 UITextField 获得焦点时为视图的位置设置动画。为此,我们必须使 UIViewController 成为 UITextField 的委托。

您需要实现的第一个委托方法是 - (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];
}

这是取自我最喜欢的 Cocoa 博客之一,可可与爱。名字很俗气,但 Matt 发表了一些关于 Objective-C 开发的精彩帖子。

Here is some code that I have used. Basically, what we're going to do is animate the position of the view whenever a UITextField gets focus. To do this, we must make our UIViewController a delegate of our UITextFields

The first delegate method you need to implement is - (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];
    }
}

Now that the view has been 'pushed up' when a UITextField is selected, we want to make sure that we slide it back down when we're done:

- (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 from one of my favorite Cocoa blogs, Cocoa With Love. Cheezy name, but Matt's got some great posts on Objective-C development.

静待花开 2024-10-16 12:59:26

特别是对于 UITextView (不是 UITextField),

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

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

Specifically for UITextView (not UITextField)

you may have a look at the following tutorial: http://codingcluster.blogspot.in/2012/03/iphone-make-uitextview-move-up-when.html

i have implemented it and it is working as expected(at least for me)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文