iOS解决键盘遮挡问题
2016-11-05 11:07
429 查看
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for(int i = 0; i < 10; i++)
{
UITextField textF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50 i + 50, 200, 45)];
textF.borderStyle = UITextBorderStyleRoundedRect;
textF.delegate = self;
textF.placeholder = [NSString stringWithFormat:@”第%d个textField”,i];
[self.view addSubview:textF];
}
//频道:UIKeyboardWillShowNotification 监听键盘即将升起
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//键盘收起的时候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];
}
(void)keyboardWillShow:(NSNotification *)noti
{
//键盘最后停止时候的高度
//得到键盘的信息
NSDictionary *dic = noti.userInfo;
//键盘停止时候的值
NSValue *keyBoardValue = [dic objectForKey:UIKeyboardFrameEndUserInfoKey];
//转为CGRect变量
CGRect keyBoardFrame = keyBoardValue.CGRectValue;
//1.获取textField底部y坐标
//2.算textField和键盘y坐标的差值
//差值代表被遮住的部分
NSInteger offset = _selectTextF.frame.origin.y + _selectTextF.frame.size.height - keyBoardFrame.origin.y;
//如果遮住
if (offset > 0) {
//视图上移动差值
self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);
}
}
(void)keyboardWillHidden
{
//键盘即将下落的时候,让self.view回到起始位置
self.view.frame = self.view.bounds;
}
(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.view endEditing:YES];
}
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
_selectTextF = textField;
return YES;
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for(int i = 0; i < 10; i++)
{
UITextField textF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50 i + 50, 200, 45)];
textF.borderStyle = UITextBorderStyleRoundedRect;
textF.delegate = self;
textF.placeholder = [NSString stringWithFormat:@”第%d个textField”,i];
[self.view addSubview:textF];
}
//频道:UIKeyboardWillShowNotification 监听键盘即将升起
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//键盘收起的时候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];
}
(void)keyboardWillShow:(NSNotification *)noti
{
//键盘最后停止时候的高度
//得到键盘的信息
NSDictionary *dic = noti.userInfo;
//键盘停止时候的值
NSValue *keyBoardValue = [dic objectForKey:UIKeyboardFrameEndUserInfoKey];
//转为CGRect变量
CGRect keyBoardFrame = keyBoardValue.CGRectValue;
//1.获取textField底部y坐标
//2.算textField和键盘y坐标的差值
//差值代表被遮住的部分
NSInteger offset = _selectTextF.frame.origin.y + _selectTextF.frame.size.height - keyBoardFrame.origin.y;
//如果遮住
if (offset > 0) {
//视图上移动差值
self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);
}
}
(void)keyboardWillHidden
{
//键盘即将下落的时候,让self.view回到起始位置
self.view.frame = self.view.bounds;
}
(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.view endEditing:YES];
}
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
_selectTextF = textField;
return YES;
}
相关文章推荐
- iOS解决呼出键盘遮挡页面问题(装逼模式一)
- iOS开发之优秀开源框架IQKeyboardManager,零行代码解决键盘遮挡问题,如此简单解决键盘遮挡问题,IQKeyboardManager源码分解
- ios解决webview键盘遮挡问题
- 【iOS 0 行代码系列】之 0 行代码解决键盘遮挡问题
- 解决iOS键盘弹出遮挡输入框问题
- iOS 解决autolayout下键盘弹出遮挡问题
- iOS项目开发键盘弹出遮挡输入框问题解决方案
- 『零行代码』解决键盘遮挡问题(iOS)
- ios--解决键盘遮挡UITextField控件的问题(方式二)
- iOS解决键盘遮挡输入框的问题
- 解决移动端 ios 系统键盘遮挡的问题
- iOS解决呼出键盘遮挡页面问题(装逼模式一)
- 『零行代码』解决键盘遮挡问题(iOS)
- 『零行代码』解决键盘遮挡问题(iOS)
- iOS解决键盘遮挡TextFiled的问题
- iOS解决键盘弹出遮挡输入框问题
- iOS 解决键盘遮挡问题
- iOS学习——键盘弹出遮挡输入框问题解决方案
- iOS开发笔记之解决键盘遮挡UITextField问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题