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

时间:2013-04-20 16:17:34

标签: iphone ios objective-c uipickerview uiactionsheet

我想

  1. 显示选择器视图(向上滑动)
  2. 在可见的情况下停用背景
  3. 显示(UIActionSheet)按钮位于底部(不在顶部)
  4. 在我看来,这是一个简单的解决方案,因为您可以找到用于在网络中的任何位置向操作表添加选择器视图的代码,但所有解决方案都将按钮置于顶部,我需要将按钮放在操作表的底部(对齐?)

    这可能吗?我想如果我看一下:Example

    此致

    Jeven

    EDITED(我的解决方案基于编码的爸爸解决方案)

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

    ...然后只应用iPhone 5 / iphone 4相关的视图大小限制

    编辑2: 只是另一个暗示!最初我想使用标准的Actionsheet,但不想将pickerview放在底部。我找不到如何移动按钮的方法。显然这很简单:Add UIView as subView on UIActionSheet很高兴知道;)

1 个答案:

答案 0 :(得分:4)

你有2个可能性:

选项1:大调整。

ActionSheet显示了在init期间定义的顺序的按钮。因此,在顶部放置一些虚拟按钮,取消将是唯一可见的,所有其他按钮将被选择器视图隐藏。代码(在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;

选项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)];

检查选项2是否按钮应位于操作表上,除非不会将点击分派到选择器方法。