当点击UIPickerView时,我正在向视图添加UILabel。当我自己添加UIPickerView时,它运行良好:

static const CGFloat HMCMeasurePickerHeight = 200.0;
CGRect measurePickerFrame = CGRectMake(0.0,
                                       CGRectGetHeight(self.view.window.bounds) - HMCMeasurePickerHeight,
                                       CGRectGetWidth(self.view.window.bounds),
                                       HMCMeasurePickerHeight);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:measurePickerFrame];
picker.dataSource = self;
picker.delegate = self;
picker.showsSelectionIndicator = YES;
[self.view addSubview:picker];


但是,我需要一个工具栏,因此将其包装在UIView中:

static const CGFloat HMCMeasurePickerAccessoryHeight = 44.0;
static const CGFloat HMCMeasurePickerHeight = 200.0;
CGRect measurePickerSuperviewFrame = CGRectMake(0.0,
                                                CGRectGetHeight(self.view.window.bounds) - HMCMeasurePickerHeight - HMCMeasurePickerAccessoryHeight,
                                                CGRectGetWidth(self.view.window.bounds),
                                                HMCMeasurePickerAccessoryHeight);
UIView *measurePicker = [[UIView alloc] initWithFrame:measurePickerSuperviewFrame];
CGRect measurePickerAccessoryFrame = CGRectMake(0.0,
                                                0.0,
                                                CGRectGetWidth(self.view.window.bounds),
                                                HMCMeasurePickerAccessoryHeight);
UIToolbar *measurePickerAccessory = [[UIToolbar alloc] initWithFrame:measurePickerAccessoryFrame];
measurePickerAccessory.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *rightAlignSpacer = [[UIBarButtonItem alloc]
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                     target:nil
                                     action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(dismissMeasurePicker:)];
measurePickerAccessory.items = @[rightAlignSpacer, doneButton];
[measurePicker addSubview:measurePickerAccessory];
CGRect measurePickerFrame = CGRectMake(0.0,
                                       HMCMeasurePickerAccessoryHeight,
                                       CGRectGetWidth(self.view.window.bounds),
                                       HMCMeasurePickerHeight);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:measurePickerFrame];
picker.dataSource = self;
picker.delegate = self;
picker.showsSelectionIndicator = YES;
[measurePicker addSubview:picker];
[self.view addSubview:measurePicker];


现在,当我尝试滚动UIPickerView时,什么也没有发生。我可以成功点击添加的“完成”按钮,只是UIPickerView没有响应。我尝试设置picker.userInteractionEnabled = YES,但是没有任何效果(考虑到第一个示例,它似乎默认为YES)。

最佳答案

我想问题是
measurePicker小于选择器。一世
建议您检查这一行。
NSLog测量的最后一帧
看看它的宽度和高度
返回。

关于ios - 为什么我的UIPickerView仅在没有 super View 时才响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18233356/

10-13 09:26