admin管理员组文章数量:1794759
成品app直播源码,iOS键盘弹出遮挡输入框
成品app直播源码,iOS键盘弹出遮挡输入框解决的相关代码
self.phoneInput = [UITextField new]; self.phoneInput.placeholder = @"请输入..."; [self.view addSubview:self.phoneInput]; ///键盘弹出 处理遮挡问题 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { //获取处于焦点中的view NSArray *textFields = @[self.phoneInput]; UIView *focusView = nil; for (UITextField *view in textFields) { if ([view isFirstResponder]) { focusView = view; break; } } if (focusView) { //获取键盘弹出的时间 double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //获取键盘上端Y坐标 CGFloat keyboardY = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; //获取输入框下端相对于window的Y坐标 CGRect rect = [focusView convertRect:focusView.bounds toView:[[[UIApplication sharedApplication] delegate] window]]; CGPoint tmp = rect.origin; CGFloat inputBoxY = tmp.y + focusView.frame.size.height; //计算二者差值 CGFloat ty = keyboardY- inputBoxY; NSLog(@"position keyboard: %f, inputbox: %f, ty: %f", keyboardY, inputBoxY, ty); //差值小于0,做平移变换 [UIView animateWithDuration:duration animations:^{ if (ty < 0) { self.view.transform = CGAffineTransformMakeTranslation(0, ty); } }]; } } - (void)keyboardWillHide:(NSNotification *)notification { //获取键盘弹出的时间 double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //还原 [UIView animateWithDuration:duration animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, 0); }]; } ///<UITextFieldDelegate> ///UITextFieldDelegate编辑完成,视图恢复原状 -(void)textFieldDidEndEditing:(UITextField *)textField { self.view.frame =CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height); }以上就是 成品app直播源码,iOS键盘弹出遮挡输入框解决的相关代码,更多内容欢迎关注之后的文章
版权声明:本文标题:成品app直播源码,iOS键盘弹出遮挡输入框 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686525556a77746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论