textView、textField键盘遮挡
2017-01-03 18:49
357 查看
最近在做一个文本录入的app,多处需要处理键盘遮挡的问题,在这里对用到的方法进行整理
1.单个文本框输入遮挡,可以通过键盘通知方法里进行计算移动视图
- (void)viewDidLoad { } 方法里添加键盘监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeContentViewPosition:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backViewPosition:)
name:UIKeyboardWillHideNotification
object:nil];
键盘显示和键盘收回的方法里做如下处理
- (void) changeContentViewPosition:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect frame = pwView.frame;
int offset = frame.origin.y + 50 - (self.view.frame.size.height - keyboardSize.height);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
-(void)backViewPosition:(NSNotification *)notification
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
最后移除通知
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
2.tableViewCell包含textField 在键盘出现和收回方法里做如下处理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - keyBoardRect.size.height);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
}
textField数据保存
cell.textFiled.delegate = self;
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *text = textField.text;
NSString *textIndexPath = [NSString stringWithFormat:@"%ld",(long)textField.tag];
//写一个字典用来保存数据
[self.textDict setObject:text forKey:textIndexPath];
}
3.tableView里既有textField还有textView,在使用第二个方法时发现textView输入时并不会移动视图,后来发现tableViewController已经帮我们处理好了
@property (nonatomic,strong)UITableView *tableView;
[self.view addSubview:self.tableView];
- (UITableView *)tableView {
if (!_tableView) {
UITableViewController* tvc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:tvc];
[tvc.view setFrame:self.view.frame];
_tableView=tvc.tableView;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
return _tableView;
}将tableView替换成tableViewController即可
本文仅做记录和个人参考,不对之处还望大家指出,共勉
1.单个文本框输入遮挡,可以通过键盘通知方法里进行计算移动视图
- (void)viewDidLoad { } 方法里添加键盘监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeContentViewPosition:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backViewPosition:)
name:UIKeyboardWillHideNotification
object:nil];
键盘显示和键盘收回的方法里做如下处理
- (void) changeContentViewPosition:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect frame = pwView.frame;
int offset = frame.origin.y + 50 - (self.view.frame.size.height - keyboardSize.height);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
-(void)backViewPosition:(NSNotification *)notification
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
最后移除通知
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
2.tableViewCell包含textField 在键盘出现和收回方法里做如下处理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - keyBoardRect.size.height);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
}
textField数据保存
cell.textFiled.delegate = self;
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *text = textField.text;
NSString *textIndexPath = [NSString stringWithFormat:@"%ld",(long)textField.tag];
//写一个字典用来保存数据
[self.textDict setObject:text forKey:textIndexPath];
}
3.tableView里既有textField还有textView,在使用第二个方法时发现textView输入时并不会移动视图,后来发现tableViewController已经帮我们处理好了
@property (nonatomic,strong)UITableView *tableView;
[self.view addSubview:self.tableView];
- (UITableView *)tableView {
if (!_tableView) {
UITableViewController* tvc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:tvc];
[tvc.view setFrame:self.view.frame];
_tableView=tvc.tableView;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
return _tableView;
}将tableView替换成tableViewController即可
本文仅做记录和个人参考,不对之处还望大家指出,共勉
相关文章推荐
- iOS 处理键盘遮挡TextField、TextView问题
- Xcode_7 iOS_9 textField和textView 关闭键盘 Objective-C (1)
- iOS开发 键盘遮挡输入框(TextView、TextField)问题
- 用TPKeyboardAvoiding(Scroll,Collection,Table,text)View避免键盘遮盖住了textfield,textview
- iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框
- iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
- RecyclerView listview 内嵌Edittext被键盘遮挡
- ios如何实现被键盘遮挡时,带有textfield的tableview自动上移
- textView键盘遮挡问题
- textView键盘被遮挡问题
- iOS 被键盘遮挡时,带有textfield的tableview自动上移
- [ios]TableView的Cell中有Textfield时放置键盘遮挡输入框
- 返回键 隐藏、、收起键盘textView|textField
- 在textView中输入时避免键盘的遮挡
- 在textView中输入时避免键盘的遮挡
- 键盘遮挡控件(textfield/textview.......)
- UITableView cell 包含textField 避免键盘遮挡
- UItableView中含输入框(TextField)被键盘遮挡问题
- 在textView中输入时避免键盘的遮挡
- 如何实现被键盘遮挡时,带有textfield的tableview自动上移(转自cocoaChina)