UITextFieldをキーボードの上に移動する方法のメモ

(iOS 5で試しています。)

TextFieldを画面の下の方に配置すると、

入力するときキーボードに隠れてしまう。

という場合には、

キーボード通知

・UIKeyboardWillShowNotification

・UIKeyboardWillHideNotification

を利用して、TextFieldの位置を変えてみましょう。

サンプルコード

@interface ViewController ()

@property (nonatomic, strong) UITextField *field;

@end

@implementation ViewController

@synthesize field;

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 背景

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    

    

    // Text Field を作成

    float height = self.view.frame.size.height;

    float width = self.view.frame.size.width;

    self.field = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width – 50, 30)];

    self.field.borderStyle = UITextBorderStyleRoundedRect;

    self.field.center = CGPointMake(self.view.center.x, height – 30);

    [self.view addSubview:self.field];

    

    

    // Text Fieldの位置をキーボードの表示にあわせて変える

    // keyboard 表示、非表示の通知設定

    [[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 *)notification

{

    CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    

    // UIViewAnimationCurve を UIViewAnimationOptionに変換

    UIViewAnimationOptions animationOptions = animationCurve << 16;

    

    // keyboard の上に TextField を移動する

    [UIView animateWithDuration:animationDuration 

                          delay:0.0 

                        options:animationOptions 

                     animations:^{

                         self.field.center = CGPointMake(self.field.center.x, keyboardEndFrame.origin.y40);

                     } 

                     completion:^(BOOL finished) {}];

    

}

– (void)keyboardWillHide:(NSNotification *)notification

{

    

    NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    

    // UIViewAnimationCurve を UIViewAnimationOptionに変換

    UIViewAnimationOptions animationOptions = animationCurve << 16;

    

    // 元の位置に戻す

    [UIView animateWithDuration:animationDuration 

                          delay:0.0 

                        options:animationOptions 

                     animations:^{

                         float height = self.view.frame.size.height;

                         self.field.center = CGPointMake(self.view.center.x, height – 30);

                     } 

                     completion:^(BOOL finished) {}];

    

}

– (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    

    [self setField:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end