點選任意View彈出Picker

weixin_33850890發表於2017-07-04

在做專案的過程中,會有些點選某個View彈出選擇器(UIPickerView)的需求(如同下圖),如果這個View是UITextField或者UITextView這樣可編輯的View還好說,直接就可以撥出鍵盤,然後直接操作inputView就好,那麼蛋疼的來了。。點選非可編輯文字的View撥出選擇器(UIPickerView)怎麼辦?下面就分享下我是如何解決這個問題的。。


6322969-2c619bc6e30e419c.png
圖示--文章說的就是它

方案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'


6322969-7b4082b06c9c164d.png
用ActionSheetPicker出來的效果

OK,這就是目前我所掌握的點選任意View彈出Picker的三種方法啦。。第一種可能有些扯淡,對於對樣式UI要求比較高的公司也是種不錯的選擇。

相關文章