为什么我的UIPickerView只在不在superview中时才会响应?

时间:2013-08-14 13:39:06

标签: ios objective-c uikit

点击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)。

1 个答案:

答案 0 :(得分:1)

我猜问题是帧大小 measurePicker小于选择器。一世 建议你查一下这条线。 NSLog是measurePicker的最后一帧 看看它的宽度和高度 回报。