display done button on UIPickerview

后端 未结 7 1818
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 02:26

I have written the following code in the viewDidLoad method:

categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[sel         


        
相关标签:
7条回答
  • 2020-12-01 03:04

    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/

    0 讨论(0)
提交回复
热议问题