Hiding/ Showing UIPickerView

后端 未结 8 584
广开言路
广开言路 2020-12-05 11:09

I Have an a touchesEnded event that checks for when a UITextField is pressed. What I would like it to do is is hide/show a UIPickerView. How can this be done?



        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 11:20

    Below is the sample code to hide and show of UIPickerView with animation:-

    //For Displaying

    -(void)showPickerView{
        self.pickerSheet = [UIAlertController alertControllerWithTitle:@"Select font" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
        self.pickerView.showsSelectionIndicator = YES;
        [self.pickerView selectRow:1 inComponent:0 animated:YES];
        [self.pickerSheet.view addSubview:self.pickerView];
        self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
        UIView *view = self.pickerView;
        [self.pickerSheet.view addConstraints:[NSLayoutConstraint
                                   constraintsWithVisualFormat:@"V:|[view]|"
                                   options:0l
                                   metrics:nil
                                   views:NSDictionaryOfVariableBindings(view)]];
    
        [self.pickerSheet.view addConstraints:[NSLayoutConstraint
                                   constraintsWithVisualFormat:@"H:|[view]|"
                                   options:0l
                                   metrics:nil
                                   views:NSDictionaryOfVariableBindings(view)]];
        [self presentViewController:self.pickerSheet animated:YES completion:^{
        }];
    
    }
    

    //For hiding

    -(void)hidePickerView{
        [self.pickerSheet dismissViewControllerAnimated:YES completion:^{
        }];
    }
    

提交回复
热议问题