1、鍵值監聽app
[[NSNotificationCenter defaultCenter] addObserver:selfide
selector:@selector(keyboardWillShow:)server
name:UIKeyboardWillShowNotificationget
object:nil];it
//增長監聽,當鍵退出時收出消息io
[[NSNotificationCenter defaultCenter] addObserver:selfemail
selector:@selector(keyboardWillHide:)object
name:UIKeyboardWillHideNotificationselect
object:nil];top
#pragma mark ------- Action
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//獲取鍵盤的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyHeight = keyboardRect.size.height;
self.view.top=100-keyHeight;
// if ([_emailText isFirstResponder]) {
// if(kDeviceHeight<_emailText.bottom+keyHeight+64)
// self.view.top=64+(kDeviceHeight-_emailText.bottom-keyHeight-64);
// }
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
//獲取鍵盤的高度
self.view.top=64;
}
2、手勢監聽
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tap1.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap1];
#pragma mark----點擊空白處收起鍵盤
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
[self.view endEditing:YES];
}