Sian 发表于 2014-9-29 13:55:02

自定义PickView,一行代码即可搞定选择器!

1、先上效果图:
   

2、使用说明:
该控件可以广泛应用于各种选择场景,这里以TextField为例,只需要一行代码即可在需要选择控件的TextField中应用
;轻松搞定!

3、源代码
SAPickView.h
//
//SAPickView.h
//
//Created by Sian on 14-9-29.
//Copyright (c) 2014年 Sian. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SAPickView : UIView

@property (nonatomic, strong)   UIPickerView    *pickView;

@property (nonatomic, strong)   NSArray         *dataArray;

@property (nonatomic, strong)   UITextField   *textField;

- (id)initWithArray:(NSArray *)array;

+ (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField;

- (void)show;

@end

SAPickView.m
//
//SAPickView.m
//
//Created by Sian on 14-9-29.
//Copyright (c) 2014年 Sian. All rights reserved.
//

#import "SAPickView.h"

@interface SAPickView () <UIPickerViewDataSource, UIPickerViewDelegate>
{
    UIToolbar   *_toolBar;
    CGSize      _size;
}
@end
@implementation SAPickView

- (id)initWithArray:(NSArray *)array
{
    if (self = ) {
      self.alpha = 0;
      self.dataArray = array;
      _size = [ bounds].size;
      self.backgroundColor = ;
      ;
    } return self;
}

- (void)addsubViews
{
    self.frame = (CGRect){CGPointZero, _size};
    [[[ delegate] window] addSubview:self];
   
    _toolBar = [ init];
    _toolBar.tintColor = kGrayColor(1.0);
    _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
    ;
   
    // 按钮自定义
    UIBarButtonItem *item1 = ;
    UIBarButtonItem *item2 = [ initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item3 = ;
    _toolBar.items = ;
   
    self.pickView = [ init];
    self.pickView.showsSelectionIndicator = YES;
    self.pickView.backgroundColor = kGrayColor(1.0);
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
    ;
}

+ (void)pickViewWithArray:(NSArray *)array forTextField:(UITextField *)textField
{
    SAPickView *pickView = [ initWithArray:array];
    pickView.textField = textField;
    ;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.dataArray;
}

- (void)show
{
    [UIView animateWithDuration:0.3 animations:^{
      self.alpha = 1;
      _toolBar.frame = (CGRect){0, _size.height - 206, _size.width, 44};
      self.pickView.frame = (CGRect){0, _size.height - 206 + 44, _size.width, 206 - 44};
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
      self.alpha = 0;
      _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
      self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
    } completion:^(BOOL finished) {
      ;
    }];
}

- (void)done
{
    self.textField.text = ];
    ;
}
@end


页: [1]
查看完整版本: 自定义PickView,一行代码即可搞定选择器!