您的位置:首页 > 其它

scrollview,键盘遮挡输入框,上移

2017-07-21 13:31 239 查看
//
//  ViewController.m
//  键盘上移
//
//  Created by lcy on 16/7/21.
//  Copyright © 16年 NJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic,assign)CGFloat keyboardHeight;//键盘高度

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

_toptextF.delegate = self;
_middleTextF.delegate = self;
_bottomTextF.delegate = self;
// 手势回收键盘
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardDown)];
[self.view addGestureRecognizer:tap];
// 监听键盘弹出,获取键盘的高度
NSNotificationCenter * defaultCenter =  [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

}

//UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
int screenH =  [UIScreen mainScreen].bounds.size.height;
// 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 -(屏幕高度- 键盘高度 - 键盘上tabbar高度/状态栏的高度)
// ✅textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量
int offset = frame.origin.y + CGRectGetHeight(textField.frame) -( screenH - _keyboardHeight - 20);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];

if(offset > 0)      // >0,说明键盘挡住
{
[UIView animateWithDuration:0.4 animations:^{
self.backScrollView.contentOffset = CGPointMake(0, offset);
}];
}
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSTimeInterval animationDuration = 0.30f;

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

[UIView setAnimationDuration:animationDuration];
[UIView animateWithDuration:0.4 animations:^{
self.backScrollView.contentOffset = CGPointMake(0, 0);
}];

[UIView commitAnimations];
}

-(void)keyboardDown{
[self.view endEditing:YES];
}

-(void)keyboardWillShow:(NSNotification *)aNotification{
//获取键盘的高度

NSDictionary *userInfo = [aNotification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

_keyboardHeight = keyboardRect.size.height;
}
@end





[b]①以下几种键盘类型几乎一样,键盘高度也是一样的

[/b]

UIKeyboardTypeAlphabet

UIKeyboardTypeASCIICapable

UIKeyboardTypeDefault

UIKeyboardTypeEmailAddress

UIKeyboardTypeNamePhonePad

UIKeyboardTypeNumbersAndPunctuation(数字和标点符号)

UIKeyboardTypeTwitter

UIKeyboardTypeURL

UIKeyboardTypeWebSearch


5.5吋271

4.7吋258

4.0吋253

②以下几种键盘为数字类型的键盘,键盘高度也是一样的

UIKeyboardTypeDecimalPad(带小数点的数字键盘)

UIKeyboardTypeNumberPad(纯数字键盘)

UIKeyboardTypePhonePad(带*+#,;的数字键盘)



5.5吋226

4.7吋216

4.0吋216
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: