简体   繁体   English

自定义UIPickerView上方的UIToolbar没有收到触摸

[英]UIToolbar above custom UIPickerView not receiving touches

I've subclassed UIPickerView to add some a little more functionality(I'm 99% sure it has nothing to do with this question). 我对UIPickerView进行了子类化,以添加一些其他功能(我99%确信它与该问题无关)。 In drawRect I added a toolbar to make dismissing the toolbar a little easier, the problem is, neither the UIToolbar, or the UIBarButtonItem inside the toolbar receive touches. drawRect中,我添加了一个工具栏,以使其更易于使用,问题是UIToolbar或工具栏内的UIBarButtonItem都不会受到触摸。 It's almost as if the view is "invisible" in that the touches are forwarded to the view behind it(a UITableView). 视图几乎是“不可见的”,因为触摸被转发到了它后面的视图(UITableView)。 I know I could just make a "control" view that holds both the picker an a toolbar. 我知道我可以制作一个“控制”视图,以同时包含选择器和工具栏。 But I just wanted to know if there was any way to do this without creating another view? 但是我只是想知道是否有任何方法可以在不创建另一个视图的情况下进行此操作?

Here's my drawRect code: 这是我的drawRect代码:

- (void)drawRect:(CGRect)rect
{

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        pickerToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width,  40)];


        UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self.delegate action: closeAction];

        [pickerToolbar setItems: [NSArray arrayWithObject: closeButton]];

        [self addSubview: pickerToolbar];
    }
}

Here's a photo: 这是一张照片:

我的自定义UIPicker

Never, never do this. 永远不要,永远不要这样做。 You are adding a new subview every time the picker is drawn! 每次绘制选择器时,您都将添加一个新的子视图!

If your picker view is the input view of a text field, simply create a toolbar and add it as the input accessory view. 如果选择器视图是文本字段的输入视图,则只需创建一个工具栏并将其添加为输入附件视图。 It will then appear above the picker for you. 然后它将显示在选择器上方。 This is the standard way to achieve this behaviour. 这是实现此行为的标准方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM