FROM http://pingguohe.net/2011/08/21/keyboard-show/

正确推出键盘和消失的方法

在iOS5中苹果对中文输入法进行了升级。在拼音模式下,键盘的上方增加了一条候选栏。在不少程序中键盘弹出后,拼音输入法的候选栏遮住了输入框。
造成这个问题的原因是因为,很多程序在处理键盘弹出和隐藏的的时候,过分简单的把键盘的高度写成了一个常量。然后把输入框推高了一个定死的高度。
在iOS5中这个方法就行不通了,如果把view推高的高度设成是中文键盘的高度。那么在英文输入状态下。键盘和其他的view之间就会流出一段候选栏高度的空白。


其实用官方的文档中就有键盘推出的示例代码:
Moving Content That Is Located Under the Keyboard

示例工程地址

 

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // Observe keyboard hide and show notifications to resize the text view appropriately.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
 
- (void)viewDidUnload {
    [super viewDidUnload];
 
    self.textView = nil;
    self.accessoryView = nil;
 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
 
- (void)viewWillAppear:(BOOL)animated {
 
    // Make the keyboard appear when the application launches.
    [super viewWillAppear:animated];
    [textView becomeFirstResponder];
}
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
 
#pragma mark -
#pragma mark Text view delegate methods
 
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {
 
    /* You can create the accessory view programmatically (in code), in the same nib file as the view controller's main view, or from a separate nib file. This example illustrates the latter; it means the accessory view is loaded lazily -- only if it is required. */
 
    if (textView.inputAccessoryView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"AccessoryView" owner:self options:nil];
        // Loading the AccessoryView nib file sets the accessoryView outlet.
        textView.inputAccessoryView = accessoryView;
        // After setting the accessory view for the text view, we no longer need a reference to the accessory view.
        self.accessoryView = nil;
    }
 
    return YES;
}
 
- (BOOL)textViewShouldEndEditing:(UITextView *)aTextView {
    [aTextView resignFirstResponder];
    return YES;
}
 
#pragma mark -
#pragma mark Responding to keyboard events
 
- (void)keyboardWillShow:(NSNotification *)notification {
 
    /* Reduce the size of the text view so that it's not obscured by the keyboard. Animate the resize so that it's in sync with the appearance of the keyboard. */
 
    NSDictionary *userInfo = [notification userInfo];
 
    // Get the origin of the keyboard when it's displayed.
    NSValue* aValue = [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 = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
 
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
 
    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
 
    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
 
    textView.frame = newTextViewFrame;
 
    [UIView commitAnimations];
}
 
- (void)keyboardWillHide:(NSNotification *)notification {
 
    NSDictionary* userInfo = [notification userInfo];
 
    /* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
 
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
 
    textView.frame = self.view.bounds;
 
    [UIView commitAnimations];
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 zer931 的頭像
    zer931

    zer931的部落格

    zer931 發表在 痞客邦 留言(0) 人氣()