ToolBar位于xcode中UIPIckerView的顶部?

时间:2012-12-04 10:15:26

标签: iphone xcode uipickerview uitoolbar

我需要在toolbar的顶部添加一个带有完成按钮的UIPickerView。我不想使用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的顶部)?

6 个答案:

答案 0 :(得分:27)

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

textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;

答案 1 :(得分:6)

您可以使用此

获取Picker
-(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);
}  

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

答案 2 :(得分: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];
}

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

好...仅在情节提要编辑器上创建垂直堆栈视图,在上方添加工具栏,在下方添加pickerview怎么样?它们不必彼此真正关联,只需要同时显示和隐藏即可。有点像下面这样:

Hierarchy

enter image description here

这真的很容易,而且您不必编写魔术代码。

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

答案 5 :(得分:-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];
        }
    }