本文主要是介绍[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]列表选择以及键盘遮挡输入框问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!