点击键盘 试图上移动

- (void)viewDidLoad {

[super viewDidLoad];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

// Do any additional setup after loading the view from its nib.

}



#pragma mark 如果键盘遮挡视图 向上移动视图

- (void)keyboardWillShow:(NSNotification *)note{

NSDictionary *info = [note userInfo];

CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

CGFloat keyBoardHeight = keyboardSize.height;

CGRect frame = self.sendButton.frame;

int offset = frame.origin.y -  (self.view.frame.size.height -keyBoardHeight)+20;

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

if(offset > 0)

self.view.frame = CGRectMake(0.0f, -offset+23, self.view.frame.size.width, self.view.frame.size.height);

[UIView commitAnimations];

}

#pragma mark 编辑完成之后视图恢复位置

- (void)textFieldDidEndEditing:(UITextField *)textField {

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

[UIView commitAnimations];

}

- (void)dealloc

{

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];

}

你可能感兴趣的:(点击键盘 试图上移动)