当iOS 7显示键盘时,如何重新调整UITextView的大小 [英] How to re-size UITextView when keyboard shown with iOS 7

查看:93
本文介绍了当iOS 7显示键盘时,如何重新调整UITextView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,它包含一个全屏 UITextView 。当显示键盘时,我想调整文本视图的大小,使其不会隐藏在键盘下。

I have a view controller which contains a full-screen UITextView. When the keyboard is shown I would like to resize the text view so that it is not hidden under the keyboard.

这是一种相当标准的iOS方法,如这个问题:

This is a fairly standard approach with iOS, as described in this question:

如何在键盘出现时调整iOS上的UITextView大小?

然而,对于iOS 7,如果用户点击文本视图在屏幕的下半部分,当文本视图调整大小时,光标保持在屏幕外。当用户点击进入时,文本视图仅滚动以将光标移动到视图中。

However, with iOS 7, if the user taps on the text view in the bottom half of the screen, when the text view resizes, the cursor remains offscreen. The text view only scrolls to bring the cursor into view if when the user hits enter.

推荐答案

虽然@Divya给出了答案引导我找到正确的解决方案(所以我授予赏金),这不是一个非常明确的答案!这里详细介绍:

Whilst the answer given by @Divya lead me to the correct solution (so I awarded the bounty), it is not a terribly clear answer! Here it is in detail:

确保文本视图不被屏幕键盘隐藏的标准方法是在显示键盘时更新其框架,详情如下:

The standard approach to ensuring that a text view is not hidden by the on-screen keyboard is to update its frame when the keyboard is shown, as detailed in this question:

如何在键盘出现时调整iOS上的UITextView大小?

但是,对于iOS 7,如果更改文本在 UIKeyboardWillShowNotification 通知的处理程序中查看框架,光标将保持在屏幕外,如本问题所述。

However, with iOS 7, if you change the text view frame within your handler for the UIKeyboardWillShowNotification notification, the cursor will remain off screen as described in this question.

解决此问题的方法是更改​​文本视图框以响应 textViewDidBeginEditing 委托方法:

The fix for this issue is to change the text view frame in response to the textViewDidBeginEditing delegate method instead:

@implementation ViewController {
    CGSize _keyboardSize;
    UITextView* textView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    textView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 20.0, 20.0)];    textView.delegate = self;
    textView.returnKeyType = UIReturnKeyDone;
    textView.backgroundColor = [UIColor greenColor];
    textView.textColor = [UIColor blackColor];
    [self.view addSubview:textView];


    NSMutableString *textString = [NSMutableString new];
    for (int i=0; i<100; i++) {
        [textString appendString:@"cheese\rpizza\rchips\r"];
    }
    textView.text = textString;

}

- (void)textViewDidBeginEditing:(UITextView *)textView1 {
    CGRect textViewFrame = CGRectInset(self.view.bounds, 20.0, 20.0);
    textViewFrame.size.height -= 216;
    textView.frame = textViewFrame;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGRect textViewFrame = CGRectInset(self.view.bounds, 20.0, 20.0);
    textView.frame = textViewFrame;
    [textView endEditing:YES];
    [super touchesBegan:touches withEvent:event];
}

@end

注意:遗憾的是 textViewDidBeginEdting UIKeyboardWillShowNotification 通知之前触发,因此需要硬编码键盘高度。

NOTE: unfortunately textViewDidBeginEdting fires before the UIKeyboardWillShowNotification notification, hence the need to hard-code the keyboard height.

这篇关于当iOS 7显示键盘时,如何重新调整UITextView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆