- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/软键盘弹出时,页面做调整
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
CGRect cashTextFieldFrame = [self.cashTextField convertRect:self.cashTextField.frame fromView:window];
[UIView animateWithDuration:duration animations:^{
if (keyboardF.origin.y < -cashTextFieldFrame.origin.y + 150) {
[self setContentOffset:CGPointMake(0.0, -cashTextFieldFrame.origin.y + 150 - keyboardF.origin.y)];
} else {
[self setContentOffset:CGPointMake(0.0, 0.0)];
}
}];
}