2013-01-09 50 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在編輯區嘗試新代碼 –