Objective C implementing a UIPickerView with a “Done” button

后端 未结 7 673
一向
一向 2020-12-03 08:56

I am trying to implement a \"Done\" button in a UIPickerView Similar to the one under this link

I looked in the class reference but I couldn t find it

Thank

7条回答
  •  醉梦人生
    2020-12-03 09:31

    The easiest way to do it is to model it in Interface Builder. It is a UIView containing a UIToolbar and a UIPickerView.

    enter image description here

    Then create an outlet for the UIView and connect it.

    enter image description here

    If you then have a UITextField you can assign your custom view to its inputView property.

    [self.textField setInputView:self.customPicker];
    

    Alternatively you can add the picker to your main view...

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.customPicker.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame), CGRectGetWidth(self.customPicker.frame), CGRectGetHeight(self.customPicker.frame));
        [self.view addSubview:self.customPicker];
    }
    

    ... and then use this method to show or hide the picker.

    - (void)setPickerHidden:(BOOL)hidden
    {
        CGAffineTransform transform = hidden ? CGAffineTransformIdentity : CGAffineTransformMakeTranslation(0, -CGRectGetHeight(self.customPicker.frame));
    
        [UIView animateWithDuration:0.3 animations:^{
            self.customPicker.transform = transform;
        }];
    }
    

提交回复
热议问题