出现取消按钮,但完成按钮未出现在 uitoolbar 中

Posted

技术标签:

【中文标题】出现取消按钮,但完成按钮未出现在 uitoolbar 中【英文标题】:cancel button appears but done button does not appear in uitoolbar 【发布时间】:2013-01-09 09:06:12 【问题描述】:

我已经编写了下面的代码,以便在 uitoolbar 的弹出窗口中显示日期选择器。 我可以看到取消按钮,但看不到完成按钮。

如果我删除 flexspace 的代码,则会出现完成按钮,但我需要完成按钮位于最右侧,但它位于取消按钮旁边。

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

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

【问题讨论】:

你做addSubView了吗? 【参考方案1】:

试试这个代码:-

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

【讨论】:

我试过了还是一样 我将粘贴整个代码,包括弹出框 另一个附加问题如何在点击完成按钮后获取所选日期。 检查此网址***.com/questions/5748752/…。此处仅来自发件人,您必须输入 cast 并将其设为 UIDatePicker,然后使用 datePicker.date,您将在此处获得结果

以上是关于出现取消按钮,但完成按钮未出现在 uitoolbar 中的主要内容,如果未能解决你的问题,请参考以下文章

UIActivityViewController共享消息取消未显示

弹出窗口未出现在 iOS 中

按钮未出现在 iOS 10 中

出现错误“由于表单未连接而取消表单提交”

应用程序上的后退按钮未出现

在我取消选中收音机并将所有变量设置为未定义后,我的弹出元素将再次出现