提高键盘按下文本字段

时间:2015-11-21 14:51:00

标签: ios objective-c iphone

当我按下一个确定的UITextField时,我想提高我的键盘,所以我创建了一个触地操作,但是在第一次调用它没有工作时,通过第二次调用它工作正常。 这里是代码(当然keyboardSize是一个CGSize类型的全局变量):

- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];     
}  

- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
 keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

}


- (void)keyboardWillHide:(NSNotification *)notification {
[self moveFrameToVerticalPosition:0.0f forDuration:0.3f];
}


- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration {
CGRect frame = self.view.frame;
frame.origin.y = position;

[UIView animateWithDuration:duration animations:^{
    self.view.frame = frame;
}];
}

- (IBAction)insert:(id)sender {

float newVerticalPosition = - keyboardSize.height;

[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.0f];

}

1 个答案:

答案 0 :(得分:0)

keyboardWillShow:添加: [self moveFrameToVerticalPosition:-keyboardSize.height forDuration:0.0f]; 你真的不需要insert: