ToolBar at the top of UIPIckerView in xcode?

26,062

Solution 1

You can getPicker using this

-(void)getValuePicker
{
    ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];

    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnPressToGetValue)];

    [toolBar setItems:[NSArray arrayWithObject:btn]];
    [ViewForValuePicker addSubview:toolBar];


    valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
    valuePicker.delegate=self;
    valuePicker.dataSource=self;
    valuePicker.showsSelectionIndicator=YES;

    [ViewForValuePicker addSubview:valuePicker];

    [appDelegate.window addSubview:ViewForValuePicker];
} 

And its Delegete Method

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [pickerValueAry count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
    id str=[ary objectAtIndex:row];
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{    
    NSLog(@"selectedRowInPicker >> %d",row);
}  

You can follow my answer for more Link

Solution 2

There is a simpler way You can insert the picker as inputView of your text Field and add the toolbar as inputAccessoryView to your textField like:

textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;

Solution 3

in viewDidLoad just add this code and when textFieldBegin just change frame like bellow...

First Create global UIView For back View of Date Picker in .h file like bellow..

UIView *viewPicker;
UIPickerView * pickerView;

and then use it like bellow..

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    viewPicker.frame = CGRectMake(0, 112, 320, viewPicker.frame.size.height);  
    [UIView commitAnimations];
    return YES;
}

add this bellow code...

    viewPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 480, 320, 258)];
    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.frame=CGRectMake(0,0,320,44);
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(doneClicked:)];


    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
    pickerView = [[UIPickerView alloc]init] ;
    pickerView.frame=CGRectMake(0, 44, 320,216);
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;

    [viewPicker addSubview:pickerView];
    [viewPicker addSubview:toolbar];
    [self.view addSubview:viewPicker];

and in Done button clicked just set again frame like this..

-(IBAction)doneClicked:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    viewPicker.frame = CGRectMake(0, 370, 320, 258);
    [UIView commitAnimations];
}

i hope this help you...

Solution 4

You could try with this code:

- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
    [textField resignFirstResponder];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 180, 260)];

    pickerView = [[UIPickerView alloc]init] ;
    pickerView.frame=CGRectMake(0, 44, 180,216);
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.frame=CGRectMake(0,0,180,44);
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(doneClicked:)];


    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

    textField.inputAccessoryView = toolbar;

    [view addSubview:toolbar];
    [view addSubview:pickerView];
    [self.view addSubview:view];

    return YES;
}
Share:
26,062
Sindhia
Author by

Sindhia

Updated on July 30, 2022

Comments

  • Sindhia
    Sindhia almost 2 years

    I need to add a toolbar with done button on the top of UIPickerView. I don't want to use actionSheet because I want the rest of the view to be active. I've gone for the following code:

    - (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
      [txtstate resignFirstResponder];
      pickerView = [[UIPickerView alloc]init] ;
      pickerView.frame=CGRectMake(10, 75, 180,20);
      pickerView.delegate = self;
      pickerView.showsSelectionIndicator = YES;
    
      UIToolbar* toolbar = [[UIToolbar alloc] init];
      toolbar.frame=CGRectMake(0,75,180,10);
      toolbar.barStyle = UIBarStyleBlackTranslucent;
      UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    
      UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                           style:UIBarButtonItemStyleDone target:self
                                                                          action:@selector(doneClicked:)];
    
    
       [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
    
       textField.inputAccessoryView = toolbar;
       [pickerView addSubview:toolbar];
       [self.view addSubview:pickerView];
    }
    

    By using the above code my toolbar is added but it is hidden under UIPickerView and it is getting added in the middle. How can I make the toolbar come to the front (on the top of UIPickerView) ?