[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

相关文章

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

解决Cron定时任务中Pytest脚本无法发送邮件的问题

《解决Cron定时任务中Pytest脚本无法发送邮件的问题》文章探讨解决在Cron定时任务中运行Pytest脚本时邮件发送失败的问题,先优化环境变量,再检查Pytest邮件配置,接着配置文件确保SMT... 目录引言1. 环境变量优化:确保Cron任务可以正确执行解决方案:1.1. 创建一个脚本1.2. 修

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C