没有出现在正确的位置

没有出现在正确的位置

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

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

最佳答案

使用以下代码

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

关于ios - UIPickerview没有出现在正确的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15384598/

10-11 16:14