0

很抱歉问了这么一个愚蠢的问题,但我不知道为什么我的 UIPickerView 和完成按钮没有出现在正确的位置。我正在使用以下代码,但我不知道为什么会遇到这样的问题。

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


UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];

[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped:)];
[barItems addObject:cancelBtn];

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

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];
[barItems addObject:doneBtn];

[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];


//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;


[actionSheet addSubview:maritalStatusPickerView];

[actionSheet showInView:self];

在此处输入图像描述

4

4 回答 4

1

使用下面的代码

actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.frame = CGRectMake(0, 234, 320, 256);

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];

[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped:)];
[barItems addObject:cancelBtn];

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

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];
[barItems addObject:doneBtn];

[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];


//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;
//        [self addSubview:maritalStatusPickerView];

[actionSheet addSubview:maritalStatusPickerView];

[self.view addSubview:actionSheet];
于 2013-03-13T12:11:13.973 回答
0

为什么要添加 maritalStatusPickerView 2 次

[self addSubview:maritalStatusPickerView];

[actionSheet addSubview:maritalStatusPickerView];
于 2013-03-13T11:58:37.273 回答
0

初始化后只需将操作表的框架包含到您的代码中

actionSheet.frame = CGRectMake(0, 0, 320, 256);

参考这个问题

于 2013-03-13T12:16:02.770 回答
0

您必须在视图中添加您的操作表,如果您想从视图底部打开选择器视图,请更改坐标。

[actionSheet addSubview:maritalStatusPickerView];

//Replace this :  [actionSheet showInView:self];

[self.view addSubview:actionSheet];
于 2013-03-13T12:18:20.587 回答