[IOS]列表选择以及键盘遮挡输入框问题

2024-08-20 23:38

本文主要是介绍[IOS]列表选择以及键盘遮挡输入框问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[IOS]列表选择以及键盘遮挡输入框问题


DOMO:http://download.csdn.net/detail/u012881779/8716639

没有仔细验证,domo里面应该会存在逻辑问题。

关于键盘遮挡问题,这里先分享一个最简单的办法:

1、把所有UITextField放到一个固定高度的UIView上面;

2、将上面的UIView放入一个UIScollView里面,并对这个UIScrollView在Xib上进行适配;

3、在代码中设置ScollView的ContentSize: 

[UIScollView setContentSize:CGSizeMake(UIScollView.frame.size.width, UIView.frame.size.height+240)];
这样需要填那个项目就自个滚去...



DOMO里的方法

控制器部分:

#import "QuestionProblemVC.h"
#import "QuestionTableView.h"@interface QuestionProblemVC ()< UIAlertViewDelegate , UITextFieldDelegate , UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton    *boyBut;           //男
@property (weak, nonatomic) IBOutlet UIButton    *girlBut;          //女
@property (weak, nonatomic) IBOutlet UITextField *ageText;          //年龄
@property (weak, nonatomic) IBOutlet UITextField *categoryText;     //类别
@property (weak, nonatomic) IBOutlet UIButton    *ageDownBut;       //年龄下拉
@property (weak, nonatomic) IBOutlet UIButton    *categoryDownBut;  //类别下拉
@property (weak, nonatomic) IBOutlet UIButton    *ageTapBut;        //年龄点击
@property (weak, nonatomic) IBOutlet UIButton    *categoryTapBut;   //类别点击
@property (weak, nonatomic) IBOutlet UITextView  *questionText;     //问题
@property (weak, nonatomic) IBOutlet UITextField *nameText;         //姓名
@property (weak, nonatomic) IBOutlet UITextField *phoneText;        //联系方式
@property (weak, nonatomic) IBOutlet UIView      *backImgV;
@property (strong, nonatomic) IBOutlet UIView    *lineView;
@property (strong, nonatomic)QuestionTableView   *questionTV;       //选择列表
@property (nonatomic) NSInteger sexInt; //性别@end@implementation QuestionProblemVC
@synthesize sexInt = _sexInt;
@synthesize questionTV = _questionTV;- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{[self.view endEditing:YES];//隐藏选择列表[self hidenTableView];[self reductionAction];
}- (void)viewDidLoad {[super viewDidLoad];[_questionText.layer setCornerRadius:3];[_questionText.layer setBorderWidth:1];[_questionText.layer setBorderColor:[[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1] CGColor]];_ageDownBut.selected = YES;_categoryDownBut.selected = YES;//点击男[self boyTapAction:nil];//通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectAgeAndCategory:) name:@"selectAgeAndCategory" object:nil];
}//判断字符串不全为空
-(BOOL)judgeStringIsNull:(NSString *)string{BOOL result = NO;if(string != nil && string.length > 0){for (int i = 0; i < string.length; i ++) {NSString *subStr = [string substringWithRange:NSMakeRange(i, 1)];if(![subStr isEqualToString:@" "] && ![subStr isEqualToString:@""]){result = YES;}}}return result;
}//确定
- (IBAction)sureAction:(id)sender {[self.view endEditing:YES];NSString *sexTemp = @"";if(_sexInt == 1){sexTemp = @"男";}else if(_sexInt == 2){sexTemp = @"女";}BOOL result = YES;if(![self judgeStringIsNull:_ageText.text] && result){result = NO;}if(![self judgeStringIsNull:_categoryText.text] && result){result = NO;}if(![self judgeStringIsNull:_questionText.text] && result){result = NO;}if(![self judgeStringIsNull:_nameText.text] && result){result = NO;}if(![self judgeStringIsNull:_phoneText.text] && result){result = NO;}if(result){UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"输入完成" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];alertv.tag = 220;[alertv show];}else{UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"请输入完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];[alertv show];}
}//点击男
- (IBAction)boyTapAction:(id)sender {_sexInt = 1;[_boyBut setImage:[UIImage imageNamed:@"选择.png"] forState:UIControlStateNormal];[_girlBut setImage:[UIImage imageNamed:@"未选择.png"] forState:UIControlStateNormal];
}//点击女
- (IBAction)grilTapAction:(id)sender {_sexInt = 2;[_boyBut setImage:[UIImage imageNamed:@"未选择.png"] forState:UIControlStateNormal];[_girlBut setImage:[UIImage imageNamed:@"选择.png"] forState:UIControlStateNormal];
}//年龄点击
- (IBAction)ageTapAction:(id)sender {[self.view endEditing:YES];_categoryDownBut.selected = YES;[_categoryDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];if(_ageDownBut.isSelected){_ageDownBut.selected = NO;[_ageDownBut setImage:[UIImage imageNamed:@"上拉按钮.png"] forState:UIControlStateNormal];[self tableVeiwMark:1 andView:_ageText];}else{_ageDownBut.selected = YES;[_ageDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];[self hidenTableView];}
}//类别点击
- (IBAction)categoryTapAction:(id)sender {[self.view endEditing:YES];_ageDownBut.selected = YES;[_ageDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];if(_categoryDownBut.isSelected){_categoryDownBut.selected = NO;[_categoryDownBut setImage:[UIImage imageNamed:@"上拉按钮.png"] forState:UIControlStateNormal];[self tableVeiwMark:2 andView:_categoryText];}else{_categoryDownBut.selected = YES;[_categoryDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];[self hidenTableView];}
}//选择列表
- (void)tableVeiwMark:(NSInteger)mark andView:(UIView *)theView{[_questionTV.view removeFromSuperview];_questionTV = nil;if(!_questionTV){_questionTV = [[QuestionTableView alloc] initWithNibName:@"QuestionTableView" bundle:nil];}_questionTV.markInt = mark;CGRect ageTextRect = theView.frame;ageTextRect.origin.y = [theView superview].frame.origin.y + ageTextRect.size.height;ageTextRect.size.height = 0;[_questionTV.view setFrame:ageTextRect];[self.view addSubview:_questionTV.view];CGRect endRect = _questionTV.view.frame;if(mark == 2){endRect.size.height = 270;}else{endRect.size.height = 250;}[UIView animateWithDuration:0.2 animations:^{[_questionTV.view setFrame:endRect];} completion:^(BOOL finished){}];}//隐藏选择列表
- (void)hidenTableView{_categoryDownBut.selected = YES;[_categoryDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];_ageDownBut.selected = YES;[_ageDownBut setImage:[UIImage imageNamed:@"下拉按钮.png"] forState:UIControlStateNormal];CGRect endRect = _questionTV.view.frame;endRect.size.height = 0;[UIView animateWithDuration:0.2 animations:^{[_questionTV.view setFrame:endRect];} completion:^(BOOL finished){[_questionTV.view removeFromSuperview];_questionTV = nil;}];
}//通知
- (void)selectAgeAndCategory:(id)sender{NSString *mark = [[sender userInfo] objectForKey:@"mark"];NSString *selectStr = [[sender userInfo] objectForKey:@"select"];if([mark isEqualToString:@"1"]){[_ageText setText:selectStr];}else if([mark isEqualToString:@"2"]){[_categoryText setText:selectStr];}[self hidenTableView];
}#pragma mark UIALertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{if(alertView.tag == 220){if(buttonIndex == 0){_categoryText.text = @"";_questionText.text = @"";}}else if(alertView.tag == 221){}
}#pragma mark UITextFieldDelegate 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{if(textField.tag == 1006){if([[UIScreen mainScreen] bounds].size.height>480){}else{[self risingAction];}}if(textField.tag == 1007){if([[UIScreen mainScreen] bounds].size.height>568){}else{[self risingAction];}}return YES;
}#pragma mark UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{if(textView.tag == 1005){if([[UIScreen mainScreen] bounds].size.height>480){}else{[self risingAction];}}return YES;
}-(void)risingAction{CGRect backRect = _backImgV.frame;backRect.origin.y = -115;[UIView animateWithDuration:0.2 animations:^{[_backImgV setFrame:backRect];} completion:^(BOOL finished){}];
}-(void)reductionAction{if([[UIScreen mainScreen] bounds].size.height>568){return;}CGRect backRect = _backImgV.frame;backRect.origin.y = 0;[UIView animateWithDuration:0.2 animations:^{[_backImgV setFrame:backRect];} completion:^(BOOL finished){}];
}@end

列表部分:

#import <UIKit/UIKit.h>@interface QuestionTableView : UITableViewController <UITableViewDataSource , UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray *ageArr;
@property (strong, nonatomic) NSMutableArray *categoryArr;
@property (nonatomic) NSInteger markInt;@end@implementation QuestionTableView
@synthesize markInt = _markInt;
@synthesize ageArr = _ageArr;
@synthesize categoryArr = _categoryArr;- (void)viewDidLoad {[super viewDidLoad];if(_markInt == 1){if(!_ageArr){_ageArr = [[NSMutableArray alloc] init];}[_ageArr addObject:@"18岁以下"];[_ageArr addObject:@"19岁-28岁"];[_ageArr addObject:@"29岁-38岁"];[_ageArr addObject:@"39岁-48岁"];[_ageArr addObject:@"49岁-58岁"];[_ageArr addObject:@"59岁-68岁"];[_ageArr addObject:@"69岁-78岁"];[_ageArr addObject:@"80岁以上"];}else if(_markInt == 2){if(!_categoryArr){_categoryArr = [[NSMutableArray alloc] init];}[_categoryArr addObject:@"普通内科"];[_categoryArr addObject:@"心脑血管"];[_categoryArr addObject:@"内分泌糖尿病"];[_categoryArr addObject:@"呼吸消化"];[_categoryArr addObject:@"肿瘤血液"];[_categoryArr addObject:@"中医"];[_categoryArr addObject:@"泌尿男科"];[_categoryArr addObject:@"妇产生殖"];[_categoryArr addObject:@"外科皮肤"];[_categoryArr addObject:@"儿科"];[_categoryArr addObject:@"五官"];[_categoryArr addObject:@"其它"];}[self.tableView setBounces:NO];
}#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if(_markInt == 1){return _ageArr.count;}else if(_markInt == 2){return  _categoryArr.count;}return 0;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}cell.selectionStyle = UITableViewCellSelectionStyleNone;cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];if(_markInt == 1){cell.textLabel.text = [_ageArr objectAtIndex:indexPath.row];}else if(_markInt == 2){cell.textLabel.text = [_categoryArr objectAtIndex:indexPath.row];}[cell.textLabel setTextColor:[UIColor blackColor]];[cell.textLabel setFont:[UIFont systemFontOfSize:15]];return cell;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 30;
}#pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];NSString *text = cell.textLabel.text;NSDictionary *nfDict = [[NSDictionary alloc] initWithObjectsAndKeys:text,@"select",[NSString stringWithFormat:@"%ld",(long)_markInt],@"mark",nil];NSNotification *notf = [[NSNotification alloc] initWithName:@"selectAgeAndCategory" object:nil userInfo:nfDict];[[NSNotificationCenter defaultCenter] postNotification:notf];
}@end


图示:



这篇关于[IOS]列表选择以及键盘遮挡输入框问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091504

相关文章

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决