- (void)viewWillAppear:(BOOL)animated
{
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
[IQKeyboardManager sharedManager].enable = NO;
self.automaticallyAdjustsScrollViewInsets = YES;
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
[self.QRCodeTextField resignFirstResponder];
[IQKeyboardManager sharedManager].enable = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = CSWhiteColor;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidChanged:) name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillDisAppear:) name:UIKeyboardWillHideNotification object:nil];
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tapGr.numberOfTapsRequired = 1;
tapGr.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGr];
}
#pragma mark - NSNotificationAction
- (void)keyBoardDidAppear:(NSNotification *)noti {
//改变self.view的bounds 需要获取键盘高度
if (self.QRCodeTextField.isFirstResponder) {
CGRect bounds = self.view.bounds;
bounds.origin.y = [self offsetHeight:[self keyboardHeightFromNoti:noti]] + bounds.origin.y;
self.view.bounds = bounds;
} else {
CGRect bounds = self.view.bounds;
bounds.origin.y = 0;
self.view.bounds = bounds;
}
}
- (void)keyBoardDidChanged:(NSNotification *)noti {
if (self.QRCodeTextField.isFirstResponder) {
CGRect bounds = self.view.bounds;
bounds.origin.y = [self offsetHeight:[self keyboardHeightFromNoti:noti]] + bounds.origin.y;
self.view.bounds = bounds;
} else {
CGRect bounds = self.view.bounds;
bounds.origin.y = 0;
self.view.bounds = bounds;
}
}
- (void)keyBoardWillDisAppear:(NSNotification *)noti {
CGRect bounds = self.view.bounds;
bounds.origin.y = 0;
self.view.bounds = bounds;
}
- (CGFloat)keyboardHeightFromNoti:(NSNotification *)noti {
NSDictionary *info = [noti userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
return keyboardSize.height;
}
- (CGFloat)offsetHeight:(CGFloat)keyboardHeight {
CGRect window = [UIScreen mainScreen].bounds;
CGRect textfield = [self.BindingBtn convertRect:CGRectMake(0, 0, self.BindingBtn.frame.size.width, self.BindingBtn.frame.size.height) toView:nil];
return MAX(0, keyboardHeight + textfield.size.height + textfield.origin.y + - window.size.height);
}
#pragma mark - 点击背景
-(void)tapAction:(UITapGestureRecognizer*)tapGr {
if (self.QRCodeTextField.isFirstResponder) {
[self.QRCodeTextField resignFirstResponder];
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
网友评论