从笔尖加载的视图未获得触摸事件

Kri*_*dra 0 iphone user-interaction interface-builder xib ios

我使用一些按钮创建了一个自定义键盘。我使用界面构建器创建了它们,然后创建了一个CustomKeyboardView文件..

所以我有3个文件

  1. 自定义键盘视图.h
  2. 自定义键盘视图.m
  3. 自定义键盘视图.xib

我正在将这个视图添加到我的UIViewController喜欢这个

-(void)createCustomKeyboard{
    //kbdCustom is an instance of CustomKeyboardView
    kbdCustom       =  [[[NSBundle mainBundle] loadNibNamed:@"CustomKeyBoardView" 
                             owner:self options:nil] objectAtIndex:0];
    kbdCustom.delegate   =   self;
    CGRect frame         =   kbdCustom.frame;
    frame.origin.x       =   14.0;
    frame.origin.y       =   300.0;
    kbdCustom.frame      =   frame;
    kbdCustom.backgroundColor   =   [UIColor clearColor];
    [self.view addSubview:kbdCustom];
    [self.view bringSubviewToFront:kbdCustom];
}
Run Code Online (Sandbox Code Playgroud)

我在屏幕上看到自定义键盘。但我无法触摸任何按钮。

CustomKeyBoardView.m文件中没有什么花哨的 。我有一些按钮处理程序来处理键盘中每种不同类型的按钮。

自定义键盘视图.m

@implementation CustomKeyBoardView
- (IBAction)numberPressed:(id)sender {
   NSLog(@"Number pressed");
}

- (IBAction)specialCharsPressed:(id)sender {
   NSLog(@"specialCharsPressed");
}

- (IBAction)clearTextPressed:(id)sender {
   NSLog(@"clearTextPressed");
}

- (IBAction)enterButtonPressed:(id)sender {
   NSLog(@"enterButtonPressed");
}
@end
Run Code Online (Sandbox Code Playgroud)

其他一些可能对您有帮助的要点(帮助我)

  1. 我检查并重新检查了按钮处理程序。它们连接到 IBAction.
  2. 我以编程方式在键盘上添加了一个按钮(通过覆盖 中的awakeFromNib函数完成CustomKeyboardView.m),当我将它添加到UIViewController. 但该按钮上也没有用户交互。
  3. UIViewController问题被示出为 UIPopOverController。我们在我们的项目中使用故事板。
  4. 我已经添加了一些日志来查看是否在kbdCustom其超级视图中启用了用户交互。答案是肯定的

    NSLog(@"kbdCustom.userInteractionEnabled : %d"
       , kbdCustom.userInteractionEnabled);
    NSLog(@"superView.userInteractionEnabled : %d"
       , kbdCustom.superView.userInteractionEnabled);
    
    Run Code Online (Sandbox Code Playgroud)

    它的输出是

    kbdCustom.userInteractionEnabled : 1
    superView.userInteractionEnabled : 1
    
    Run Code Online (Sandbox Code Playgroud)
  5. 当然,所有按钮上的用户交互都是YES,并且所有这些都在界面构建器中启用。

  6. 我已经调整了键盘的框架,将其向上移动到 UITextField(正确接收触摸事件)上。现在,当我单击键盘按钮时,它下方的 UITextField 正在被触摸,就好像键盘不在那里一样。

rpt*_*thi 5

检查视图的自动调整大小,并设置它的 layer clipsToBound,然后查看它是否仍然可见。如果不是,则意味着您的视图由于 缩小到最小尺寸autoSizing,但它的子视图显示超出了您的baseView. 在这种情况下,您可以看到它们但不能触摸。

  • 同上。同样的问题,但使用自动布局并且无法让我的 xib 识别触摸事件。 (2认同)