[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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明