// Call this method somewhere in your view controller setup code. - (void)viewDidLoad { [self registerForKeyboardNotifications]; [super viewDidLoad]; } - (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 點下ipad的隱藏鍵盤鍵觸發 - (void)keyboardWasHidden:(NSNotification*)aNotification { [userField resignFirstResponder]; //必須的 [passwordTxt resignFirstResponder]; [UIView beginAnimations:@"LoginViewController" context:nil]; [UIView setAnimationDuration:0.5]; self.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height); [UIView commitAnimations]; }