如何创建一个完全适合键盘的UITextView?

时间:2012-02-17 22:15:54

标签: iphone ios cocoa-touch ios5

我想创建一个视图,例如Facebook或Twitter应用程序的共享对话框,例如,只有UITextView和永久键盘。我可以看到如何从this answer可见的键盘开始,但我不确定如何调整UITextView的大小以恰好适合键盘上方。如果我不这样做,文字就会隐藏在键盘下面,这很尴尬。

5 个答案:

答案 0 :(得分:3)

您可以订阅UIKeyboardWillShowNotification。通知包括键盘框架,因此您可以调整视图大小以适应剩余空间。

答案 1 :(得分:3)

我在Apple文档中找到了一个有用的代码示例:https://developer.apple.com/library/ios/#samplecode/KeyboardAccessory/Introduction/Intro.html

这是我最终得到的代码。我并不担心键盘隐藏,因为在我看来,键盘永远不会隐藏。

- (void)viewWillAppear:(BOOL)flag
{
    [super viewWillAppear:flag];

    // Listen for the keyboard to show up so we can get its height
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    // Set focus on the text view to display the keyboard immediately
    [self.textView becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];

    // Get the origin of the keyboard when it's displayed.
    NSValue* keyboardFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    // Get the top of the keyboard as the y coordinate of its origin in self's view's
    // coordinate system. The bottom of the text view's frame should align with the
    // top of the keyboard's final position.
    CGRect keyboardRect = [keyboardFrame CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    // Set the text view's frame height as the distance from the top of the view bounds
    // to the top of the keyboard
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
    self.textView.frame = newTextViewFrame;
}

答案 2 :(得分:2)

您可以像这样获得键盘尺寸:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardIsUp:) name:UIKeyboardDidShowNotification object:nil];

- (void)keyboardIsUp:(NSNotification *)notification{

    CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
    NSLog(@"%f", keyboardSize.height);
}

[编辑]横向模式

我在iPas模拟器上尝试了它,它在纵向模式下为QWERTY键盘返回264但是当你启动应用程序或旋转到横向模式时它返回1024.所以你可能需要要求宽度而不是高度景观模式......

[编辑]

感谢rob mayoff的评论,景观模式已经没有问题了

<强> [编辑]

这不是最好的方法,但这提供了一个想法。我稍后会回顾一下

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

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

    CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
    CGFloat width =  size.width;
    CGFloat height = 40;
    CGFloat x =  0;
    CGFloat y =  size.height+40;

    aboveKBView = [[[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
    [aboveKBView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:aboveKBView];
}

- (void)keyboardWillHide:(NSNotification *)notification{
    [aboveKBView setHidden:YES];
}

- (void)keyboardWillShow:(NSNotification *)notification{
    NSLog(@"keyboardIsUp");

    CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;

    CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
    CGFloat width =  size.width;
    CGFloat height = 40;
    CGFloat x =  0;
    CGFloat y =  size.height-(keyboardSize.height+height);

    [aboveKBView setFrame:CGRectMake(x, y, width, height)];
    [aboveKBView setHidden:NO];    
}

答案 3 :(得分:0)

我觉得这个页面总是非常有用 - 它显示了所有重要的尺寸,因此很容易计算出你需要的尺寸:

http://www.idev101.com/code/User_Interface/sizes.html

答案 4 :(得分:0)

试试这个

在viewDidLoad中:

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

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

并添加这些方法。

- (void) keyboardWillShow: (NSNotification*) aNotification;
{   

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];

    CGRect rect = [[self view] frame];

    rect.origin.y -= 60; 

    [[self view] setFrame: rect];

    [UIView commitAnimations];

}

- (void) keyboardWillHide: (NSNotification*) aNotification;
{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];

    CGRect rect = [[self view] frame];

    rect.origin.y += 60; 

    [[self view] setFrame: rect];

    [UIView commitAnimations];
}