iOS开发 键盘遮挡输入框(TextView、TextField)问题
2013-06-20 14:22
1926 查看
首先在.h文件中要添加UITextFieldDelegate ,UIGestureRecognizerDelegate,UITextViewDelegate
这三个协议。
下面是.m文件中的代码
解决textview遮挡键盘代码
解决textfield键盘退出代码,当然也要用到上面textviewDidEndEditing
解决textfield遮挡键盘代码
恢复屏幕,也是textfield键盘退出功能
这三个协议。
下面是.m文件中的代码
- (void)viewDidLoad { [super viewDidLoad]; //指定本身为代理 self.textfield.delegate = self; self.textView.delegate = self; //添加手势,点击屏幕其他区域关闭键盘的操作 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)]; gesture.numberOfTapsRequired = 1; gesture.delegate = self; [self.view addGestureRecognizer:gesture];
}
//隐藏键盘的方法 -(void)hidenKeyboard { [self.feedBackContent resignFirstResponder]; [self.feedBackContact resignFirstResponder]; [self resumeView]; }
//恢复原始视图位置 -(void)resumeView { NSTimeInterval animationDuration=0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; //如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度 float Y = 0.0f; CGRect rect=CGRectMake(0.0f,Y,width,height); self.view.frame=rect; [UIView commitAnimations]; }
解决textview遮挡键盘代码
//UITextView的协议方法,当开始编辑时监听 -(BOOL)textViewShouldBeginEditing:(UITextView *)textView { NSTimeInterval animationDuration=0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; //上移70个单位,按实际情况设置 CGRect rect=CGRectMake(0.0f,-70,width,height); self.view.frame=rect; [UIView commitAnimations]; return YES; }当然还要恢复屏幕
- (IBAction)textViewDidEndEditing:(UITextView *)textView { [self.textview resignFirstResponder]; [self resumeView]; }
解决textfield键盘退出代码,当然也要用到上面textviewDidEndEditing
//用于解决textview键盘退出问题 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
解决textfield遮挡键盘代码
//UITextField的协议方法,当开始编辑时监听 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSTimeInterval animationDuration=0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; //上移30个单位,按实际情况设置 CGRect rect=CGRectMake(0.0f,-180,width,height); self.view.frame=rect; [UIView commitAnimations]; return YES; }
恢复屏幕,也是textfield键盘退出功能
- (IBAction)textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; [self resumeView]; }
相关文章推荐
- iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框
- [ios]TableView的Cell中有Textfield时放置键盘遮挡输入框
- UItableView中含输入框(TextField)被键盘遮挡问题
- TableView的Cell中有Textfield、TextView时放置键盘遮挡输入框
- (iOS)关于键盘遮挡textfield问题
- iOS项目开发键盘弹出遮挡输入框问题解决方案
- IOS TextField弹出键盘挡住输入框的问题
- Swift 解决IOS开发中TableView中编辑UITextField时键盘遮挡输入框的情况
- iOS开发小记:带输入框(TextField)的UIAlertView
- IOS TextField弹出键盘挡住输入框的问题(续)
- 当textField输入框被键盘遮挡时 让整个view上移
- iOS开发小记:带输入框(TextField)的UIAlertView
- iOS开发tableView的cell里面有textField,键盘出现的时候,自动上移
- iOS 处理键盘遮挡TextField、TextView问题
- iOS开发使用textView代替textField时,textView的placeholder问题
- iOS开发-多个textField情况下关闭键盘
- [新旧版本]iOS开发小记:带输入框(TextField)的UIAlertView
- 更完美的《iOS中textField、textView判断过滤禁用emoji表情》,解决过滤表情后九宫格输入不了问题
- iOS 输入框弹出键盘后改变tableView高度内容不被遮挡
- iOS解决键盘遮挡输入框的问题