ios - 为什么我的 UIPickerView 仅在不在 super View 中时才会响应?

标签 ios objective-c uikit

当点击 UILabel 时,我将向我的 View 添加 UIPickerView。当我单独添加 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 比 picker 小。我 建议你检查一下这条线。 NSLog是measurePicker的最后一帧 看看它的宽度和高度 返回。

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

相关文章:

ios - 在 iOS( objective-c )中单击 Button 时,如何在 UITableView 中获取特定的 JSON 数据?

ios - 从另一个 UIViewController 和他的事件复制 UIView

iOS:触摸 JSON 并检索数据大小

ios - 在 iOS 中将一个图像覆盖在另一个图像上

objective-c - 使用 IB 元素实例的访问器有哪些优点?

ios - 如何确定何时显示 UICollectionViewCell?

swift - 清晰的导航栏,没有半透明

ios - 未为 UIColor 定义散列 UIPlaceholderColor 需要先转换颜色空间

ios - 在 IOS 9 上使用 AVPlayer 和 Swift 反向播放视频

ios - didSelectRowAtIndexPath 不适用于 NSNotificationCenter