UITextField点击之后弹出UIPickerView,用PickerView覆盖键盘。去除第一响应。
闲话不多,直接上代码
定义:
@interface AddlandroidViewController () <UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, strong) UITextField *landroidNameTF;
@property (nonatomic, strong) UITextField *landroidNumberTF;
@property (nonatomic, strong) UIButton *donePickerBtn;
@property (strong, nonatomic) UIPickerView *landroidPicker;
布局pick view
-(UIPickerView *) landroidPicker{
if (!_landroidPicker) {
_landroidPicker = [[UIPickerView alloc] init];
self.landroidArray = [NSMutableArray arrayWithArray:@[NSLocalizedString(@"Landroid S WR102SI", nil),NSLocalizedString(@"Landroid S WR102SI.1", nil),NSLocalizedString(@"Landroid S WR103SI", nil),NSLocalizedString(@"Landroid S WR103SI.1", nil),NSLocalizedString(@"Landroid S WR104SI", nil),
NSLocalizedString(@"Landroid S WR104SI.1", nil),NSLocalizedString(@"Landroid S WR105SI", nil),NSLocalizedString(@"Landroid S WR105SI.1", nil),NSLocalizedString(@"Landroid S WR106SI", nil),NSLocalizedString(@"Landroid S WR106SI.1", nil),NSLocalizedString(@"Landroid S WR110SI", nil),NSLocalizedString(@"Landroid S WR110SI.1", nil),NSLocalizedString(@"Landroid S WR111SI", nil),]];
self.landroidPicker.dataSource = self;
self.landroidPicker.delegate = self;
[self.landroidPicker selectRow:0 inComponent:0 animated:YES];
[self.view addSubview:_landroidPicker];
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"]) {
//iPhone
[self.landroidPicker mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(ScreenHeight * 0.35);
make.width.mas_equalTo(ScreenWidth);
make.centerX.equalTo(self.view.mas_centerX);
make.top.equalTo(self.view.mas_top).offset(ScreenHeight * 0.05 + 344 + 64);
}];
}else if([deviceType isEqualToString:@"iPad"]) {
//iPad
[self.landroidPicker mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(ScreenHeight * 0.35);
make.width.mas_equalTo(ScreenWidth);
make.centerX.equalTo(self.view.mas_centerX);
make.top.equalTo(self.view.mas_top).offset(ScreenHeight * 0.01 + 344 + 64);
}];
}
}
return _landroidPicker;
}
//自定义pick view的字体和颜色
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
[pickerLabel setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
pickerLabel.textColor = [UIColor whiteColor];
}
// Fill the label text here
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
代理:
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED {
return 40;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.landroidArray.count;
}
#pragma mark - UIPickerViewDelegate
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.landroidArray[row];
}
#pragma mark - UIUITextField action
-(void)textFieldTextChange{
}
#pragma mark - resign keyboard control
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.landroidNameTF resignFirstResponder];
[self.landroidNumberTF resignFirstResponder];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.landroidNameTF resignFirstResponder];
[self.landroidNumberTF resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (_landroidNameTF == textField) {
_landroidPicker = [self landroidPicker];
[self.landroidNameTF resignFirstResponder];
_landroidPicker.hidden = NO;
//控制pickerView隐藏按钮
_donePickerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_donePickerBtn.titleLabel.font = [UIFont systemFontOfSize:18];
[_donePickerBtn setTitle:LocalString(@"Done") forState:UIControlStateNormal];
[_donePickerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_donePickerBtn addTarget:self action:@selector(hidePickerView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_donePickerBtn];
[_donePickerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100/WScale, 40/HScale));
make.right.equalTo(self.view.mas_right).offset(-30);
make.top.equalTo(self.nextBtn.mas_bottom).offset(50);
}];
}
}
- (void)hidePickerView{
NSInteger row = [self.landroidPicker selectedRowInComponent:0];
self.landroidNameTF.text = [_landroidArray objectAtIndex:row];
_landroidPicker.hidden = YES;
//[self.landroidPicker removeFromSuperview];
[self.donePickerBtn removeFromSuperview];
}