iPhone控制键盘

有时候当键盘弹出时会把输入框盖住,这时候我们就没法看到自己到底输入的是什么了,这时候我们就需要注册两个事件,那就是在键盘显示的时候 把输入框的位置放在可见的地方 ,当关闭键盘时,在将输入框还原,以下就是解决问题的具体方法,我们只要注册就可以了

// Call this method somewhere in your view controller setup code.

- (void)registerForKeyboardNotifications

{

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidShowNotification 

 object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidHideNotification 

 object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasHidden:)

name:UIKeyboardDidHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification

{

self.frame = CGRectMake(0, -90, 320, 480);

}

// Called when the UIKeyboardDidHideNotification is sent

- (void)keyboardWasHidden:(NSNotification*)aNotification

{

self.frame = CGRectMake(0, 20 , 320, 460);

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidShowNotification 

 object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 

name:UIKeyboardDidHideNotification 

 object:nil];

}


猜你喜欢

转载自374016526.iteye.com/blog/1156923