2

Toolbars buttons are not working when I am adding Toolbar on picker view. It's working in iOS 6 but not in iOS 7.

 UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0 ,0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(handleDone)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(handleCancel)];

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:self
                                                                          action:nil];

[toolBar setItems:[NSArray arrayWithObjects:cancelButton, flexible, doneButton, nil] animated:YES];

[pikerView addSubview:toolBar];
halfer
  • 19,824
  • 17
  • 99
  • 186
Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
  • use the uiview and add the toolbar and uipickerview inside the uiview. – Rushabh Feb 18 '14 at 10:35
  • http://stackoverflow.com/questions/13700552/toolbar-at-the-top-of-uipickerview-in-xcode – Rushabh Feb 18 '14 at 10:39
  • http://stackoverflow.com/questions/20883388/display-done-button-on-uipickerview/20883575#20883575 have a look at this answer given by me – Charan Giri Feb 18 '14 at 10:45
  • thanks for reply, but why it is happening I want to know.I knew that taking separate view and adding both thing separate will work. Rushabh put your reply as an answer so i can accept it.Need explanation for above question why is it so. – Sanoj Kashyap Feb 18 '14 at 10:50

1 Answers1

3

The UIToolbar with the 'Done' button should be added to the inputAccessoryView of the view that becomes first responder. As the UIView class inherits from UIResponder, any view can potentially contain an inputView and inputAccessoryView. So instead of manually performing the animations programmatically, you could use the default animation behaviour that comes with the UIResponder's keyboard show/hide animation.

  1. Subclass a UIView and override the inputView and inputAccessoryView properties and make them readwrite. In this example, I will subclass a UITableViewCell.

    // FirstResponderTableViewCell.h
    @interface FirstResponderTableViewCell : UITableViewCell
    @property (readwrite, strong, nonatomic) UIView *inputView;
    @property (readwrite, strong, nonatomic) UIView *inputAccessoryView;
    @end
    
  2. Override canBecomeFirstResponder in your subclass' implementation.

    // FirstResponderTableViewCell.m
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    
  3. In your view controller, create and assign the picker view and input accessory toolbar

    // MyViewController.m
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIPickerView *pickerView = [[UIPickerView alloc] init];
        UIToolbar *accessoryToolbar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        // Configure toolbar .....
    
        // note: myFirstResponderTableViewCell is an IBOutlet to a static cell in storyboard of type FirstResponderTableViewCell
        self.myFirstResponderTableViewCell.inputView = pickerView;
        self.myFirstResponderTableViewCell.inputAccessoryView = accessoryToolbar;
    }
    
  4. Don't forget to assign first responder to the view when required (e.g. inside - tableView:didSelectRowAtIndexPath:)

    [self.myFirstResponderTableViewCell becomeFirstResponder];
    

Hope this helps.

Reference: http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/

ken
  • 3,897
  • 3
  • 22
  • 28