點(diǎn)擊任意View彈出Picker

在做項(xiàng)目的過程中,會(huì)有些點(diǎn)擊某個(gè)View彈出選擇器(UIPickerView)的需求(如同下圖),如果這個(gè)View是UITextField或者UITextView這樣可編輯的View還好說,直接就可以呼出鍵盤,然后直接操作inputView就好,那么蛋疼的來了。。點(diǎn)擊非可編輯文字的View呼出選擇器(UIPickerView)怎么辦?下面就分享下我是如何解決這個(gè)問題的。。


圖示--文章說的就是它

方案1(比較簡單,相對比較low,但可定制性較強(qiáng)):

自定義一個(gè)View,最下面放一個(gè)UIPickerView,View的上面放取消和完成按鈕,點(diǎn)擊View的時(shí)候?qū)憘€(gè)UIView動(dòng)畫彈出來就可以了,簡單到不行,定制性也較強(qiáng)。

方案2(這篇文章主要介紹這種方法):

@property(strong,nonatomic,readwrite)UIToolbar*inputAccessoryView;

@property(strong,nonatomic,readwrite)UIPickerView*inputView;

首先要讓你定制的這個(gè)View的inputAccessoryView,和inputView這兩個(gè)屬性可讀可寫,如果不這樣做你是沒有權(quán)限來操作它們的。

下一步,重寫 -(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,到這里這個(gè)View就定制完成了,要讓這個(gè)View彈出Picker就還差最后一步了。

[view對象 becomeFirstResponder];

在點(diǎn)擊的時(shí)候調(diào)用一下becomeFirstResponder對象方法就OK了。

方案3:

在做完這個(gè)功能之后。。。發(fā)現(xiàn)有個(gè)叫ActionSheetPicker的三方。。。喜歡用三方的朋友可以用這個(gè),功能和效果都不錯(cuò)。。很流暢。。

可以通過pod安裝:

pod'ActionSheetPicker-3.0','~> 1.6.1'


用ActionSheetPicker出來的效果

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容