1 文本輸入,鍵盤顯示時,view向上,鍵盤隱藏時,view向下ide
1.1 註冊鍵盤顯示,關閉通知,並實現主界面上下變更post
[[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 { CGRect keyBoardRect=[[[aNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue]; NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame=self.view.frame; frame.origin.y=-keyBoardRect.size.height; [UIView beginAnimations:@"keyboardshow" context:nil]; [UIView setAnimationDuration:animalInterval]; self.view.frame=frame; [UIView commitAnimations]; } -(void)keyboardWillHide:(NSNotification *)aNotification { NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame=self.view.frame; frame.origin.y=0; [UIView beginAnimations:@"keyboardhide" context:nil]; [UIView setAnimationDuration:animalInterval]; self.view.frame=frame; [UIView commitAnimations]; }
1.2 文本框初始化,並實現UITextViewDelegate委託spa
self.textbox.returnKeyType=UIReturnKeyDone; self.textbox.delegate=self; } - (BOOL)textFieldShouldReturn:(UITextView *)textView { [textView resignFirstResponder]; return YES; }
2 自定義notificationcode
2.1 定義偵聽自定義notification觀察者server
//註冊觀察者,偵聽自定義通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selfNotificationDO:) name:@"CustomNotification" object:nil]; } -(void)selfNotificationDO:(NSNotification *)aNotification { //處理notification //........ }
2.2 生成一個自定義notificationblog
//生成一個自定義Notification [[NSNotificationCenter defaultCenter] postNotificationName:@"CustomNotification" object:self];