我创建了UIActionSheet并在其上添加了一个UIPickerView。现在,我想在UIActionSheet导航栏标题的右侧和左侧添加两个按钮。我怎么能这样做?
这是我到目前为止所做的:
- (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)];
}
我正在尝试以这种方式添加UIButton,但我无法弄清楚为什么我在视图中看不到它。当我检查UIActionSheet子视图时,他就在那里(可能在某处)。
- (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];
}
}
}
这就是它的样子:
我发现有一种方法可以到达UIActionSheet导航栏或工具栏(标题所在的位置)并在顶部添加按钮。
提前致谢。
根据人们的回答,我已经设法创建一个更好的选择器视图,呈现相同,但处理和构建方式更好。我已经创建了一个UIViewController,并在顶部添加了一个UIDatePicker和一个带按钮的UIToolBar。不像那样称呼它:
- (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];
}
我alsvo创建了一个代表,告诉我何时点击了取消和完成按钮:
- (void)datePickerDidCancelled:(CustomDatePicker *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)datePicker:(CustomDatePicker *)picker dateDidSelected:(NSDate *)date
{
NSLog(@"selectedDate: %@", date);
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:2)
根据documentation,您不应将子视图添加到UIActionSheets。
UIActionSheet不是设计为子类,也不应该添加 查看其层次结构。如果您需要提供更多的表格 自定义比UIActionSheet API提供的,您可以创建 你自己并以模态呈现它 presentViewController:动画:完成:
答案 1 :(得分:1)
我通常处理日期输入的方法是为控件创建自定义输入视图(例如,UILabel
或UITextField
)。我创建了自己的子类来处理这个问题,但你可以在任何地方进行临时处理。
InputView
属性设置为pickerView InputAccessoryView
属性设置为您创建的UIToolbar
或UINavigationBar
如果您要将其添加到UILabel
,请确保它可以成为第一响应者(有关说明,请参阅this link)。
如果你想获得幻想,你也可以将你的挑选视图嵌入UIInputView
,这将给你每个键盘输入在iOS中的模糊效果。这部分可能需要一些调整,但最终效果非常好。