在做项目的过程中,会有些点击某个View弹出选择器(UIPickerView)的需求(如同下图),如果这个View是UITextField或者UITextView这样可编辑的View还好说,直接就可以呼出键盘,然后直接操作inputView就好,那么蛋疼的来了。。点击非可编辑文字的View呼出选择器(UIPickerView)怎么办?下面就分享下我是如何解决这个问题的。。
方案1(比较简单,相对比较low,但可定制性较强):
自定义一个View,最下面放一个UIPickerView,View的上面放取消和完成按钮,点击View的时候写个UIView动画弹出来就可以了,简单到不行,定制性也较强。
方案2(这篇文章主要介绍这种方法):
@property(strong,nonatomic,readwrite)UIToolbar*inputAccessoryView;
@property(strong,nonatomic,readwrite)UIPickerView*inputView;
首先要让你定制的这个View的inputAccessoryView,和inputView这两个属性可读可写,如果不这样做你是没有权限来操作它们的。
下一步,重写 -(UIToolbar*)inputAccessoryView 、 -(UIPickerView*)inputView 和 - (BOOL)canBecomeFirstResponder方法:
-(UIToolbar*)inputAccessoryView{
if(!_inputAccessoryView){
UIToolbar*toolBar = [[UIToolbaralloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 44)];
toolBar.tintColor=SYSTEM_COLOR;
toolBar.backgroundColor= [UIColorcolorWithHexStr:@"$f1f1f1"];
UIBarButtonItem*left = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCanceltarget:selfaction:@selector(cancel)];
UIBarButtonItem*fix = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:@selector(fix)];
UIBarButtonItem*right = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(done)];
toolBar.items= [NSArrayarrayWithObjects:left,fix,right,nil];
returntoolBar;
}
return_inputAccessoryView;
}
-(UIPickerView*)inputView
{
if(!_inputView){
_inputView= [[UIPickerViewalloc]init];
_inputView.backgroundColor= [UIColorcolorWithHexStr:@"#f1f1f1"];
_inputView.bounds=CGRectMake(0, 0,SCREEN_WIDTH,SCREEN_HEIGHT/3);
_inputView.delegate=self;
_inputView.dataSource=self;
_inputView.showsSelectionIndicator=YES;
}
return_inputView;
}
-(void)done
{
[selfhideInputView];
if(self.delegate) {
if(![self.dataSourceobjectAtIndex:self.selectIndex]) {
return;
}
[self.delegateloanHeaderItem:selfdidSelectValue:[self.dataSourceobjectAtIndex:self.selectIndex]atIndex:self.selectIndex];
}
}
- (void)cancel
{
[selfhideInputView];
}
- (void)fix
{
}
- (void)showInputView
{
UIWindow* window =self.window;
self.bview= [[UIViewalloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,SCREEN_HEIGHT)];
self.bview.backgroundColor= [UIColorblackColor];
UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hideInputView)];
[self.bviewaddGestureRecognizer:tap];
self.bview.alpha= 0.3;
[windowaddSubview:self.bview];
}
- (void)hideInputView
{
[selfresignFirstResponder];
[self.bviewremoveFromSuperview];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
return1;
}
- (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(![self.dataSourceobjectAtIndex:row]) {
return@"";
}
returnself.dataSource[row];
}
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component
{
returnself.dataSource.count;
}
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectIndex= row;
}
- (BOOL)canBecomeFirstResponder
{
[selfshowInputView];
returnYES;
}
- (NSMutableArray*)dataSource
{
if(!_dataSource) {
_dataSource= [NSMutableArrayarrayWithObjects:@"1",@"2",@"3",@"5",@"6",@"7",@"8",@"9",nil];
}
return_dataSource;
}
OK,到这里这个View就定制完成了,要让这个View弹出Picker就还差最后一步了。
[view对象 becomeFirstResponder];
在点击的时候调用一下becomeFirstResponder对象方法就OK了。
方案3:
在做完这个功能之后。。。发现有个叫ActionSheetPicker的三方。。。喜欢用三方的朋友可以用这个,功能和效果都不错。。很流畅。。
可以通过pod安装:
pod'ActionSheetPicker-3.0','~> 1.6.1'
OK,这就是目前我所掌握的点击任意View弹出Picker的三种方法啦。。第一种可能有些扯淡,对于对样式UI要求比较高的公司也是种不错的选择。