display done button on UIPickerview

后端 未结 7 1844
伪装坚强ぢ
伪装坚强ぢ 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:02

    You can create view and add toolbar with "Done" button and UIPickerView as subviews

    - (void)createInputView {
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
        UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
        [toolBar setBarStyle:UIBarStyleDefault];
        UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
        UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                      style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(doneClicked)];
        toolBar.items = @[flex, barButtonDone];
        barButtonDone.tintColor = [UIColor blackColor];
    
        UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolBar.frame.size.height, screenWidth, 200)];
        picker.delegate = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
    
    
        UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
        inputView.backgroundColor = [UIColor clearColor];
        [inputView addSubview:picker];
        [inputView addSubview:toolBar];
    
        textField.inputView = inputView;
    }
    
    - (void)doneClicked {
        [textField resignFirstResponder];
    }
    

提交回复
热议问题