ToolBar at the top of UIPIckerView in xcode?

后端 未结 6 1137
清歌不尽
清歌不尽 2020-12-20 20:08

I need to add a toolbar with done button on the top of UIPickerView. I don\'t want to use actionSheet because I want the rest of the v

6条回答
  •  忘掉有多难
    2020-12-20 20:18

    in viewDidLoad just add this code and when textFieldBegin just change frame like bellow...

    First Create global UIView For back View of Date Picker in .h file like bellow..

    UIView *viewPicker;
    UIPickerView * pickerView;
    

    and then use it like bellow..

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        viewPicker.frame = CGRectMake(0, 112, 320, viewPicker.frame.size.height);  
        [UIView commitAnimations];
        return YES;
    }
    

    add this bellow code...

        viewPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 480, 320, 258)];
        UIToolbar* toolbar = [[UIToolbar alloc] init];
        toolbar.frame=CGRectMake(0,0,320,44);
        toolbar.barStyle = UIBarStyleBlackTranslucent;
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    
        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(doneClicked:)];
    
    
        [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
        pickerView = [[UIPickerView alloc]init] ;
        pickerView.frame=CGRectMake(0, 44, 320,216);
        pickerView.delegate = self;
        pickerView.showsSelectionIndicator = YES;
    
        [viewPicker addSubview:pickerView];
        [viewPicker addSubview:toolbar];
        [self.view addSubview:viewPicker];
    

    and in Done button clicked just set again frame like this..

    -(IBAction)doneClicked:(id)sender
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        viewPicker.frame = CGRectMake(0, 370, 320, 258);
        [UIView commitAnimations];
    }
    

    i hope this help you...

提交回复
热议问题