[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

相关文章

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

resultMap如何处理复杂映射问题

《resultMap如何处理复杂映射问题》:本文主要介绍resultMap如何处理复杂映射问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录resultMap复杂映射问题Ⅰ 多对一查询:学生——老师Ⅱ 一对多查询:老师——学生总结resultMap复杂映射问题

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La