문제

I created UIActionSheet and added a UIPickerView on top of it. Now, I want to add two button on the right and left of the UIActionSheet navigation bar title. How can I do that?

This is what I did so far:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select your birthday date:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
    [actionSheet showInView:self.view];
    [actionSheet setFrame:CGRectMake(0, 100, 570, 383)];
}

I'm trying to add the UIButton in this way, but I can't figure out why I can't see it on the view. When I check the UIActionSheet subviews, he is there (maybe somewhere under).

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 50, 50, 216)];

    //  Configure picker...
    [pickerView setDatePickerMode:UIDatePickerModeDate];
    [pickerView setTag:ACTION_SHEET_PICKER_TAG];

    //  Add picker to action sheet
    [actionSheet addSubview:pickerView];

    //  Add button to the action sheet
    UIButton *buttonCancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonCancel setFrame:CGRectMake(5, 100, 200, 200)];
    [buttonCancel setTitle:@"close" forState:UIControlStateNormal];
    [buttonCancel setBackgroundColor:[UIColor redColor]];
    [actionSheet addSubview:buttonCancel];

    //  Gets an array af all of the subviews of our actionSheet
    NSArray *subviews = [actionSheet subviews];
    for (id button in subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setUserInteractionEnabled:NO];
            [button setHidden:YES];
        }
    }
}

This is how it looks like:

enter image description here

I figure out of there is a way to reach the UIActionSheet navigation bar or toolbar (where the title is located) and add button on top.

Thanks in advance.


According to people answer's I've managed to create a better picker view that is presented the same, but handles and build way much better. I've created a UIViewController and added a UIDatePicker and a UIToolBar with buttons on top. Than called it like that:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    CustomDatePicker *customPicker = [[CustomDatePicker alloc] initWithNibName:@"CustomDatePicker" bundle:nil];
    customPicker.delegate = self;
    customPicker.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:customPicker animated:YES completion:nil];
}

I alsvo created a Delegate that tells me when cancel and done buttons were clicked:

- (void)datePickerDidCancelled:(CustomDatePicker *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)datePicker:(CustomDatePicker *)picker dateDidSelected:(NSDate *)date
{
    NSLog(@"selectedDate: %@", date);
    [self dismissViewControllerAnimated:YES completion:nil];
}
도움이 되었습니까?

해결책

Per the documentation, you shouldn't add subviews to UIActionSheets.

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

다른 팁

The way I usually handle date inputs is to make a custom input view for a control (for example, a UILabel or UITextField). I made my own subclass to handle this, but you can do it ad-hoc anywhere quite simply.

  1. Set the InputView property of the control to your pickerView
  2. Set the InputAccessoryView property of the control to a UIToolbar or UINavigationBar you created
  3. Add your custom buttons to the toolbar/navigation bar

If you're adding this to a UILabel, make sure it can become the First Responder (see this link for instructions).

If you want to get fancy, you can also embed your pickerview into a UIInputView, which will give you the same blurry effect every keyboards input has in iOS. This part can require some tweaks, but the final effect is pretty neat.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top