Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Demo/Classes/MMTestViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ - (IBAction)showPickerViewButtonPressed:(id)sender {
MMvalueY: @3,
MMselectedObject:_selectedString,
MMtextAlignment:@1}
selected:^(NSString *selectedString) {

_label.text = selectedString;
_selectedString = selectedString;
}
completion:^(NSString *selectedString) {

_label.text = selectedString;
_selectedString = selectedString;
}];
}];



Expand Down
7 changes: 5 additions & 2 deletions MMPickerView/MMPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ extern NSString * const MMselectedObject;
extern NSString * const MMtoolbarBackgroundImage;
extern NSString * const MMtextAlignment;
extern NSString * const MMshowsSelectionIndicator;
extern NSString * const MMspaceBottom;

@interface MMPickerView: UIView
@interface MMPickerView: UIView

+(void)showPickerViewInView: (UIView *)view
withStrings: (NSArray *)strings
withOptions: (NSDictionary *)options
selected: (void (^)(NSString *))selected
completion: (void(^)(NSString *selectedString))completion;

+(void)showPickerViewInView: (UIView *)view
withObjects: (NSArray *)objects
withOptions: (NSDictionary *)options
objectToStringConverter: (NSString *(^)(id object))converter
completion: (void(^)(id selectedObject))completion;
selected: (void (^)(NSString *))selected
completion: (void(^)(id selectedObject))completion;

+(void)dismissWithCompletion: (void(^)(NSString *))completion;

Expand Down
60 changes: 22 additions & 38 deletions MMPickerView/MMPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
NSString * const MMtoolbarBackgroundImage = @"toolbarBackgroundImage";
NSString * const MMtextAlignment = @"textAlignment";
NSString * const MMshowsSelectionIndicator = @"showsSelectionIndicator";
NSString * const MMspaceBottom = @"spaceBottom";

@interface MMPickerView () <UIPickerViewDelegate, UIPickerViewDataSource>

Expand All @@ -37,6 +38,7 @@ @interface MMPickerView () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, assign) CGFloat yValueFromTop;
@property (nonatomic, assign) NSInteger pickerViewTextAlignment;
@property (nonatomic, assign) BOOL pickerViewShowsSelectionIndicator;
@property (copy) void (^onSelect)(NSString *);
@property (copy) void (^onDismissCompletion)(NSString *);
@property (copy) NSString *(^objectToStringConverter)(id object);

Expand All @@ -59,13 +61,15 @@ + (MMPickerView*)sharedView {
+(void)showPickerViewInView:(UIView *)view
withStrings:(NSArray *)strings
withOptions:(NSDictionary *)options
selected: (void (^)(NSString *))selected
completion:(void (^)(NSString *))completion{

[[self sharedView] initializePickerViewInView:view
withArray:strings
withOptions:options];

[[self sharedView] setPickerHidden:NO callBack:nil];
[self sharedView].onSelect = selected;
[self sharedView].onDismissCompletion = completion;
[view addSubview:[self sharedView]];

Expand All @@ -75,13 +79,17 @@ +(void)showPickerViewInView:(UIView *)view
withObjects:(NSArray *)objects
withOptions:(NSDictionary *)options
objectToStringConverter:(NSString *(^)(id))converter
selected: (void (^)(NSString *))selected
completion:(void (^)(id))completion {

[self sharedView].objectToStringConverter = converter;
[self sharedView].onSelect = selected;
[self sharedView].onDismissCompletion = completion;

[[self sharedView] initializePickerViewInView:view
withArray:objects
withOptions:options];

[[self sharedView] setPickerHidden:NO callBack:nil];
[view addSubview:[self sharedView]];

Expand Down Expand Up @@ -177,10 +185,19 @@ -(void)initializePickerViewInView: (UIView *)view
//Whole screen with PickerView and a dimmed background
_pickerViewContainerView = [[UIView alloc] initWithFrame:view.bounds];
[_pickerViewContainerView setBackgroundColor: [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]];

if (options[MMspaceBottom] != nil) {

CGRect customFrame = _pickerViewContainerView.frame;
customFrame.size.height -= [options[MMspaceBottom] floatValue];

_pickerViewContainerView.frame = customFrame;
}

[self addSubview:_pickerViewContainerView];

//PickerView Container with top bar
_pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, 320.0, 260.0)];
_pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, view.frame.size.width, 260.0)];

//Default Color Values (if colors == nil)

Expand Down Expand Up @@ -210,22 +227,9 @@ -(void)initializePickerViewInView: (UIView *)view
}
_pickerViewFont = pickerViewFont;

/*
//ToolbackBackgroundImage - Clear Color
if (toolbarBackgroundImage!=nil) {
//Top bar imageView
_pickerTopBarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, _pickerContainerView.frame.size.width, 44.0)];
//[_pickerContainerView addSubview:_pickerTopBarImageView];
_pickerTopBarImageView.image = toolbarBackgroundImage;
[_pickerViewToolBar setHidden:YES];

}
*/

_pickerContainerView.backgroundColor = pickerViewBackgroundColor;
[_pickerViewContainerView addSubview:_pickerContainerView];


//Content of pickerContainerView

//Top bar view
Expand All @@ -238,16 +242,11 @@ -(void)initializePickerViewInView: (UIView *)view
[_pickerContainerView addSubview:_pickerViewToolBar];

CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
//NSLog(@"%f",iOSVersion);

if (iOSVersion < 7.0) {
_pickerViewToolBar.tintColor = toolbarBackgroundColor;
//[_pickerViewToolBar setBackgroundColor:toolbarBackgroundColor];
_pickerViewToolBar.tintColor = toolbarBackgroundColor;
}else{
[_pickerViewToolBar setBackgroundColor:toolbarBackgroundColor];

//_pickerViewToolBar.tintColor = toolbarBackgroundColor;

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
_pickerViewToolBar.barTintColor = toolbarBackgroundColor;
#endif
Expand All @@ -263,23 +262,12 @@ -(void)initializePickerViewInView: (UIView *)view
_pickerViewToolBar.items = @[flexibleSpace, _pickerViewBarButtonItem];
[_pickerViewBarButtonItem setTintColor:buttonTextColor];

//[_pickerViewBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Neue" size:23.0], UITextAttributeFont,nil] forState:UIControlStateNormal];

/*
_pickerDoneButton = [[UIButton alloc] initWithFrame:CGRectMake(_pickerContainerView.frame.size.width - 80.0, 10.0, 60.0, 24.0)];
[_pickerDoneButton setTitle:@"Done" forState:UIControlStateNormal];
[_pickerContainerView addSubview:_pickerDoneButton];
[_pickerDoneButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
*/

//Add pickerView
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 216.0)];
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, view.frame.size.width, 216.0)];
[_pickerView setDelegate:self];
[_pickerView setDataSource:self];
[_pickerView setShowsSelectionIndicator: _pickerViewShowsSelectionIndicator];//YES];
[_pickerContainerView addSubview:_pickerView];

//[self.pickerViewContainerView setAlpha:0.0];
[_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];

//Set selected row
Expand Down Expand Up @@ -310,9 +298,9 @@ - (NSString *)pickerView: (UIPickerView *)pickerView

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (self.objectToStringConverter == nil) {
self.onDismissCompletion ([_pickerViewArray objectAtIndex:row]);
self.onSelect ([_pickerViewArray objectAtIndex:row]);
} else{
self.onDismissCompletion (self.objectToStringConverter ([self selectedObject]));
self.onSelect (self.objectToStringConverter ([self selectedObject]));
}
}

Expand All @@ -334,10 +322,6 @@ - (UIView *)pickerView:(UIPickerView *)pickerView
CGRect frame = CGRectMake(0.0, 0.0, 292.0, 44.0);
customPickerView = [[UIView alloc] initWithFrame: frame];

// UIImageView *patternImageView = [[UIImageView alloc] initWithFrame:frame];
// patternImageView.image = [[UIImage imageNamed:@"texture"] resizableImageWithCapInsets:UIEdgeInsetsZero];
// [customPickerView addSubview:patternImageView];

if (_yValueFromTop == 0.0f) {
_yValueFromTop = 3.0;
}
Expand Down
61 changes: 39 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ You can show the PickerView:
```objective-c
+(void)showPickerViewInView: (UIView *)view
withStrings: (NSArray *)strings
withOptions: (NSDictionary *)options
withOptions: (NSDictionary *)options,
selected: (void(^)(NSString *selectedString))selected
completion: (void(^)(NSString *selectedString))completion;

+(void)showPickerViewInView: (UIView *)view
withObjetcs: (NSArray *)objects
withOptions: (NSDictionary *)options
objectToStringConverter: (NSString *(^)(id object))converter
completion: (void(^)(id selectedObject))completion;
selected: (void(^)(NSString *selectedString))selected
completion: (void(^)(id selectedObject))completion;
```


Expand All @@ -59,10 +61,14 @@ You can show the PickerView:
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:nil
completion:^(NSString *selectedString) {
//selectedString is the return value which you can use as you wish
self.label.text = selectedString;
}];
selected:^(NSString *selectedString) {
//selectedString is the return value when the selected item change
self.label.text = selectedString;
}
completion:^(NSString *selectedString) {
//selectedString is the return value after user hit Done button
self.label.text = selectedString;
}];
```

#### Example 2 - Show with an array of objects.
Expand All @@ -73,16 +79,19 @@ You can show the PickerView:
[MMPickerView showPickerViewInView:self.view
withObjects:objects
withOptions:nil
objectToStringConverter:^NSString *(id object) {
//This is where you convert your object and return a string, for eg. return person.name;
return [object description];

}
completion:^(id selectedObject) {
//The selected object is returned, and you can use the value as you wish
//For example: self.label.text = person.name;
self.label.text = [selectedObject description];
}];
objectToStringConverter:^NSString *(id object) {
//This is where you convert your object and return a string, for eg. return person.name;
return [object description];
}
selected:^(id selectedObject) {
//selectedObject: The selected object when the selected item change
self.label.text = [selectedObject description];
}
completion:^(id selectedObject) {
//selectedObject: The selected object after user hit Done button
//For example: self.label.text = person.name;
self.label.text = [selectedObject description];
}];
```

### Customizing MMPickerView
Expand All @@ -100,6 +109,7 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`.
- `MMselectedObject` - `id`
- `MMtoolbarBackgroundImage` - `UIImage`
- `MMtextAlignment` - `NSNumber`
- `MMspaceBottom` - `NSNumber`

```objective-c
/*
Expand All @@ -113,6 +123,7 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`.
MMselectedObject - id - The selected object presented in the PickerView, an object from the array, for eg. [yourArray objectAtIndex:0];
MMtoolbarBackgroundImage - UIImage - The background image of the toolbar (320 x 44 for non retina, 640 x 88 for retina)
MMtextAlignment - NSNumber - The text alignment of the labels in the PickerView, @0 for Left, @1 for Center, @2 for Right
MMspaceBottom - NSNumber - If the picker view is combined with a bottom bar (such as tabbar) set a MMspaceBottom with the height of bar.
*/
```

Expand All @@ -132,8 +143,12 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`.
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:options
selected:^(NSString *selectedString) {
//selectedString is the return value when the selected item change
self.label.text = selectedString;
}
completion:^(NSString *selectedString) {
//selectedString is the return value which you can use as you wish
//selectedString is the return value after user hit Done button
self.label.text = selectedString;
}];
```
Expand Down Expand Up @@ -173,11 +188,13 @@ A useful feature is to let the PickerView select whatever was selected last time
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:@{selectedObject:_selectedString}
completion:^(NSString *selectedString) {

_label.text = selectedString;
_selectedString = selectedString;
}];
selected:^(NSString *selectedString) {
_label.text = selectedString;
}
completion:^(NSString *selectedString) {
_label.text = selectedString;
_selectedString = selectedString;
}];

}

Expand Down