简体   繁体   English

将UIPickerView和UIToolBar添加到UIActionSheet时出现问题

[英]Problems with adding UIPickerView and UIToolBar to UIActionSheet

I am quite new to coding iOS, hope you can help me with this, thank you. 我对编写iOS代码非常陌生,希望您能对此提供帮助,谢谢。

I've added an UIToolBar with some UIBarButtonItem and an UIPickerView to the xib, and want to add them into an UIActionSheet in code. 我已经添加了一个UIToolBar一些UIBarButtonItemUIPickerView到厦门国际银行,并希望将其添加到UIActionSheet代码。 The UIToolBar , UIBarButtonItem , UIPickerView and UIActionSheet are properties and I do it like: UIToolBarUIBarButtonItemUIPickerViewUIActionSheet是属性,我这样做:

_pickerView.frame = CGRectMake(0, 40, 320, 216);
[_actionSheet addSubview:_toolBar];
[_actionSheet addSubview:_pickerView];

As you can see in the image below, there are some problems with it: 如您在下图中所看到的,它存在一些问题:

  1. the UIBarButtonItem s are missing UIBarButtonItem丢失
  2. the UIToolBar should be in black UIToolBar应该为黑色
  3. the UIPickerView seems too big that the right side is missing UIPickerView似乎太大,右侧丢失了

I know I can do all of this programmatically, but I think it's better to put UI stuff in xib. 我知道我可以通过编程方式完成所有这些操作,但是我认为最好将UI内容放入xib中。 Can anyone tell me how to fix it. 谁能告诉我如何解决它。

在此处输入图片说明

在此处输入图片说明

I recommend you check out UIActionSheetPicker by Tim Cinel. 我建议您查看Tim Cinel的UIActionSheetPicker。 It has really made stuff like this simpler for me, and the examples are really easy to follow and understand. 对于我来说,它确实使这样的事情变得更简单,并且这些示例确实易于理解和理解。

Here's the link: https://github.com/skywinder/ActionSheetPicker-3.0 这是链接: https : //github.com/skywinder/ActionSheetPicker-3.0

(Old Link https://github.com/TimCinel/ActionSheetPicker ) (旧链接https://github.com/TimCinel/ActionSheetPicker

This has a bar above the UIPickerView for buttons like Done/Cancel, but I have found that modifying the code to change these buttons is relatively easy as well. 它在UIPickerView上方有一个条,用于完成/取消之类的按钮,但是我发现修改代码以更改这些按钮也相对容易。

For getting the desired result which is shown in Above photo......i recomand you the Programing approach is better then the xib approch 为了获得所需的效果,如上图所示……我建议您使用编程方法胜过xib方法

i think you a re trying to implement a picker action sheet with Done button. 我认为您正在尝试通过“完成”按钮实施选择器操作表。 i will refer using segemnt control instead of using toolbar. 我将使用segemnt控件而不是工具栏进行引用。

Use this following code where you want to show action sheet with picker and Done button. 在要显示带有选择器和“完成”按钮的操作表的地方,请使用以下代码。

UIActionSheet *Example_Actionsheet = [[UIActionSheet alloc] initWithTitle:nil  delegate:self
                                                 cancelButtonTitle:nil  
                                            destructiveButtonTitle:nil
                                                 otherButtonTitles:nil];  

    Ex_Picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
    Ex_Picker.delegate = self;  
    Ex_Picker.showsSelectionIndicator = YES;    // note this is default to NO  
    Example_Actionsheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [Example_Actionsheet addSubview:Bedroom_Picker];  
    [Example_Actionsheet showInView:self.view]; 
    [Example_Actionsheet setBounds:CGRectMake(0, 0, 320, 485)];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blueColor];
    [closeButton addTarget:self action:@selector(Close_method) forControlEvents:UIControlEventValueChanged];
    [Example_Actionsheet addSubview:closeButton];
    [Example_Actionsheet release];

and if you want to use Toolbar 如果您想使用工具栏

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

NSMutableArray *barItems = [[NSMutableArray alloc] init];

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

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];

[pickerDateToolbar setItems:barItems animated:YES];

And add the toolbar and picker in subview of actionsheet. 并在操作表的子视图中添加工具栏和选择器。

[aac addSubview:"Your toolbar"];
[aac addSubview:"Your picker"];

and set the action sheet frame. 并设置动作表框架。 its done with toolbar. 它通过工具栏完成。

Following Code use for the Add Toolbar As a Subview. 以下代码用于将工具栏添加为子视图。

-(IBAction)btnOption:(id)sender{

            self.tBar.frame=CGRectMake(0, 0, 320, 44);
            self.pkrMain.frame=CGRectMake(0, 47, 320, 216);

           self.act=[[UIActionSheet alloc] initWithTitle:lblReam delegate:self cancelButtonTitle:lblOk destructiveButtonTitle:@"Cancel" otherButtonTitles:@"OptionA",@"OptionB",nil];

            [self.act addSubview:self.tBar];
            [self.act addSubview:self.pkrMain];

            [self.pkrMain reloadAllComponents];
            [self.act clipsToBounds];
            [self.act showInView:[UIApplication sharedApplication].keyWindow];
        }

finally, I added an UIView to wrap the toolbar and the picker, and then added it to the UIActionSheet in code: 最后,我添加了一个UIView来包装工具栏和选择器,然后通过代码将其添加到UIActionSheet中:

[_actionSheet addSubview:_wrapper];

the layout is fine after doing this. 完成此操作后,布局就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM