2013-01-09 55 views
0

我已经写下面的代码来显示在uitoolbar弹出日期选择器。
我可以看到取消按钮,但没有完成按钮。取消按钮出现,但完成按钮不会出现在uitoolbar

如果我删除了flexspace的代码,那么done按钮会出现,但我需要done按钮位于极右位置,但它旁边是取消按钮。

我该如何解决这个问题?提前致谢。

UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController 

    UIView *popoverView = [[UIView alloc] init]; //view 
    popoverView.backgroundColor = [UIColor blackColor]; 

    datePicker=[[UIDatePicker alloc]init];//Date picker 
    datePicker.frame=CGRectMake(0,44,320, 216); 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    [datePicker setMinuteInterval:5]; 
    [datePicker setTag:10]; 
    [datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged]; 
    [popoverView addSubview:datePicker]; 

    popoverContent.view = popoverView; 
    popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 
    popoverController.delegate=self; 



    [popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO]; 
    [popoverController presentPopoverFromRect:self.uitext.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame 




// UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)]; 
// [pickerToolbar sizeToFit]; 
// pickerToolbar.barStyle = UIBarStyleBlackTranslucent; 
// NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
//  
// UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)]; 
// [barItems addObject:cancelBtn]; 
// 
// UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
// [barItems addObject:flexSpace]; 
// 
// UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)]; 
// [barItems addObject:doneBtn]; 
//  
// 
//  
// [pickerToolbar setItems:barItems animated:YES]; 
//  




// UIPickerView *picker = [[UIPickerView alloc] init]; 
// picker.frame = CGRectMake(0, 44, 320, 216); 
// picker.delegate = self; 
// picker.dataSource = self; 
// picker.showsSelectionIndicator = YES; 
// [actionSheet addSubview:picker]; 


    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)]; 
    [pickerToolbar sizeToFit]; 
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)]; 
    [barItems addObject:cancelBtn]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)]; 
    [barItems addObject:doneBtn]; 


    [pickerToolbar setItems:barItems animated:YES]; 
     [popoverView addSubview:pickerToolbar]; 

    [self.uitext resignFirstResponder]; 
+0

你做了addSubView? –

回答

2

试试这个代码: -

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)]; 
    [pickerToolbar sizeToFit]; 
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)]; 
    [barItems addObject:cancelBtn]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)]; 
    [barItems addObject:doneBtn]; 


    [pickerToolbar setItems:barItems animated:YES]; 

    [popoverView addSubview:pickerToolbar]; 

希望它可以帮助你..

编辑: -

添加下面[pickerToolbar setItems:barItems animated:YES];此行: -

pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 

和评论[pickerToolbar sizeToFit];线

所以,你的代码将

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,44.0)]; 
    //[pickerToolbar sizeToFit]; 
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)]; 
    [barItems addObject:cancelBtn]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)]; 
    [barItems addObject:doneBtn]; 


    [pickerToolbar setItems:barItems animated:YES]; 

    pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
    [popoverView addSubview:pickerToolbar]; 
+0

我也试过它仍然是相同的我会粘贴包括弹出的整个代码 –

+0

我粘贴了整个代码。我不知道什么是错的 –

+0

@JacobWood在编辑区尝试新代码 –