6

我需要toolbarUIPickerView. 我不想使用actionSheet,因为我希望视图的其余部分处于活动状态。我已经使用了以下代码:

- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
  [txtstate resignFirstResponder];
  pickerView = [[UIPickerView alloc]init] ;
  pickerView.frame=CGRectMake(10, 75, 180,20);
  pickerView.delegate = self;
  pickerView.showsSelectionIndicator = YES;

  UIToolbar* toolbar = [[UIToolbar alloc] init];
  toolbar.frame=CGRectMake(0,75,180,10);
  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]];

   textField.inputAccessoryView = toolbar;
   [pickerView addSubview:toolbar];
   [self.view addSubview:pickerView];
}

通过使用上面的代码,我的工具栏被添加了,但它隐藏在下面UIPickerView并且被添加到了中间。我怎样才能使工具栏出现在前面(在顶部UIPickerView)?

4

6 回答 6

32

有一种更简单的方法您可以将选择器作为文本字段的 inputView 插入,并将工具栏作为 inputAccessoryView 添加到您的文本字段,例如:

textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
于 2014-12-30T14:34:13.587 回答
6

你可以使用这个 getPicker

-(void)getValuePicker
{
    ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];

    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnPressToGetValue)];

    [toolBar setItems:[NSArray arrayWithObject:btn]];
    [ViewForValuePicker addSubview:toolBar];


    valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
    valuePicker.delegate=self;
    valuePicker.dataSource=self;
    valuePicker.showsSelectionIndicator=YES;

    [ViewForValuePicker addSubview:valuePicker];

    [appDelegate.window addSubview:ViewForValuePicker];
} 

及其委托方法

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [pickerValueAry count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
    id str=[ary objectAtIndex:row];
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{    
    NSLog(@"selectedRowInPicker >> %d",row);
}  

您可以按照我的回答获取更多链接

于 2012-12-04T10:24:25.277 回答
3

只需添加此代码,viewDidLoad当 textFieldBegin 只需更改如下所示的框架...

首先在文件中为日期选择器的后视图创建​​全局 UIView,.h如下所示..

UIView *viewPicker;
UIPickerView * pickerView;

然后像下面这样使用它..

- (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;
}

添加以下代码...

    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];

并在完成按钮中单击只是再次设置这样的框架..

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

我希望这可以帮助你...

于 2012-12-04T10:29:31.457 回答
1

您可以尝试使用以下代码:

- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
    [textField resignFirstResponder];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 180, 260)];

    pickerView = [[UIPickerView alloc]init] ;
    pickerView.frame=CGRectMake(0, 44, 180,216);
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.frame=CGRectMake(0,0,180,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]];

    textField.inputAccessoryView = toolbar;

    [view addSubview:toolbar];
    [view addSubview:pickerView];
    [self.view addSubview:view];

    return YES;
}
于 2012-12-04T10:42:32.327 回答
0

好的...在故事板编辑器上创建一个垂直堆栈视图,在上面添加工具栏和下面的选择器视图怎么样?它们不必真正相互关联,只需要同时显示和隐藏即可。有点像下面这样:

等级制度

在此处输入图像描述

这真的很容易做到,你不必做编码魔术。

是的,我知道这个问题已有 7 年历史,但仍有人在寻找这个问题的解决方案。

于 2019-04-08T20:11:30.367 回答
-1

你可以这样设置你的 UIPickerView : -

-(void)showPicker{


    menu = [[UIActionSheet alloc] initWithTitle:@"Select Time"
                                       delegate:self
                              cancelButtonTitle:nil
                         destructiveButtonTitle:nil
                              otherButtonTitles:nil];

    pickerView=[[UIPickerView alloc] init];
    pickerView.frame= CGRectMake(0.0, 44.0, 0.0, 0.0);
    pickerView.delegate=self;
    pickerView.dataSource=self;
    pickerView.showsSelectionIndicator = YES;
    txtopsbox.inputView=pickerView; 

    dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    dateTool.barStyle=UIBarStyleBlackOpaque;
    [dateTool sizeToFit]; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];

    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelClicked:)];

    [barItems addObject:cancel];


    [dateTool setItems:barItems animated:YES];
    [menu addSubview:dateTool];
    [menu addSubview:pickerView];
    [menu showInView:self.view];
    [menu setBounds:CGRectMake(0,0,320, 464)];
    [pickerView release];

    }

更新

你也可以像另一种方式一样做: -

- (IBAction)showPicker
    {
        if(pickerVisible == NO)
        {
            // create the picker and add it to the view
            if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease];
            [self.datePicker setMaximumDate:[NSDate date]];
            [self.datePicker setDatePickerMode:UIDatePickerModeDate];
            [self.datePicker setHidden:NO];
            [self.view addSubview:datePicker];

            // the UIToolbar is referenced 'using self.datePickerToolbar'
            [UIView beginAnimations:@"showDatepicker" context:nil];
            // animate for 0.3 secs.
            [UIView setAnimationDuration:0.3];

            CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
            datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
            self.datePickerToolbar.frame = datepickerToolbarFrame;

            CGRect datepickerFrame = self.datePicker.frame;
            datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
            self.datePicker.frame = datepickerFrame;

            [UIView commitAnimations];
            pickerVisible = YES;
        }
    }

    - (IBAction)done
    {
        if(pickerVisible == YES)
        {
            [UIView beginAnimations:@"hideDatepicker" context:nil];
            [UIView setAnimationDuration:0.3];

            CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
            datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
            self.datePickerToolbar.frame = datepickerToolbarFrame;

            CGRect datepickerFrame = self.datePicker.frame;
            datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
            self.datePicker.frame = datepickerFrame;
            [UIView commitAnimations];

            // remove the picker after the animation is finished
            [self.datePicker performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.3];
        }
    }
于 2012-12-04T10:17:54.960 回答