display done button on UIPickerview

Viewed 52645

I have written the following code in the viewDidLoad method:

categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[self.view addSubview:categoryPickerView];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;

and called this method to hide picker view

- (IBAction)hidePickerView:(id)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
    categoryPickerView.transform = transfrom;
    categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1;
    [UIView commitAnimations];
}

My problem is that I want to display a "Done" button on a picker view and the picker view should hide on button click.

8 Answers

In Swift 5 try this,

  func setToolBar() {
        let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44))
        toolBar.barStyle = UIBarStyle.default
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.donePicker))
        toolBar.setItems([doneButton],animated: false)
        toolBar.isUserInteractionEnabled = true
        doneButton.tintColor = UIColor.black
        picker.addSubview(toolBar)
      }  

  @objc func donePicker() {
    // hide keypad
  }
Related