Вопрос

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];
Это было полезно?

Решение

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/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top