简体   繁体   English

将UIPickerView添加到UIActionSheet(底部的按钮)

[英]Adding UIPickerView to UIActionSheet (buttons at the bottom)

I want to 我想要

  1. show a picker view (sliding up) 显示选择器视图(向上滑动)
  2. deactivate the background while it's visible 在可见时停用背景
  3. show (UIActionSheet) buttons at the bottom (not at the top) 在底部(而不是顶部)显示(UIActionSheet)按钮

It seems to me an easy solution at first since you can find code for adding a picker view to an action sheet everywhere in the web but all solutions position the buttons at top and I need to put the buttons at the bottom of the action sheet (alignment?) . 一开始我觉得这是一个简单的解决方案,因为您可以找到代码以在Web上的任何地方向操作表添加选择器视图,但是所有解决方案都将按钮置于顶部,而我需要将按钮置于操作表的底部(对齐?)

Is this possible? 这可能吗? I guess it is if I look at: Example 我想是这样的: 示例

Regards 问候

Jeven Jeven

EDITED (my solution based on coded dads solution) 编辑(我的解决方案基于已编码的父亲解决方案)

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    UISegmentedControl *backButton =  [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", nil]] ;
    [backButton addTarget:self action:@selector(someFunction:) forControlEvents:UIControlEventValueChanged];
    backButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
    backButton.segmentedControlStyle = UISegmentedControlStyleBar;
    [backButton addTarget:self action:@selector(btnActionCancelClicked:) forControlEvents:UIControlEventAllEvents];
    backButton.frame = CGRectMake(20.0, 220.0, 280.0, 40.0);

    UISegmentedControl *acceptButton =  [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Accept", nil]] ;
    [acceptButton addTarget:self action:@selector(anotherFunction:) forControlEvents:UIControlEventValueChanged];
    acceptButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
    acceptButton.segmentedControlStyle = UISegmentedControlStyleBar;
    acceptButton.frame = CGRectMake(20.0, 280.0, 280.0, 40.0);

    [actionSheet addSubview:pickerView];
    [actionSheet addSubview:backButton];
    [actionSheet addSubview:acceptButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    [actionSheet setFrame:CGRectMake(0,150,320, 400)]; 

... then just apply iPhone 5/iphone 4 dependent view size constraints ...然后只需应用iPhone 5 / iphone 4相关视图大小约束

EDITED 2: Just another hint! 编辑2:另一个提示! Originally I wanted to use the standard Actionsheet, but didn't want to place the pickerview at the bottom. 最初,我想使用标准的Actionsheet,但不想将pickerview放在底部。 I didn't find a way how to move the buttons. 我没有找到一种方法来移动按钮。 Obviously it s really simple: Add UIView as subView on UIActionSheet Good to know ;) 显然,这非常简单: 在UIActionSheet上将UIView添加为subView高兴知道;)

You have 2 possibilites: 您有2种可能性:

Option1: the big tweak. 选项1:大调整。

ActionSheet shows the buttons with the order defined during init. ActionSheet显示按钮以及在初始化期间定义的顺序。 So place some dummy buttons at the top and Cancel will be the only visible, all other buttons will be hidden by the pickerview. 因此,将一些虚拟按钮放在顶部,“取消”将是唯一可见的,而其他所有按钮将被pickerview隐藏。 Code (warning-tweak at otherButtonTitles) : 代码(在otherButtonTitles上进行警告调整)

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Actionsheet"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"1", @"2", @"3", @"4", nil];

UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;

[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];

CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;

Option2: the selfmade 选项2:自制

menu = [[UIActionSheet alloc] initWithTitle:@"Actionsheet"
                                                  delegate:self
                                         cancelButtonTitle:nil
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;

UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(80,230, 170, 73);
[startbtn setBackgroundImage:[UIImage imageNamed:@"yourbutton.png"] forState:UIControlStateNormal];
[startbtn addTarget:self action:@selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];

[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,150,320, 350)];

Check that for option2 the button should be on the actionsheet unless the clicks will not be dispatched to the selector method. 检查选项2的按钮是否应该在操作表上,除非单击不会被分派到选择器方法。

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

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