【问题标题】:Custom View above a keyboard not working in ios9键盘上方的自定义视图在 ios9 中不起作用
【发布时间】:2016-02-01 16:21:16
【问题描述】:

在键盘顶部添加了一个由按钮组成的自定义视图。按钮显示正确,但在点击按钮时,会按下键盘的底层键而不是按钮操作。

UIWindow* tempWindow = [UIApplication sharedApplication].windows.lastObject;
for (UIView *keyboard in [tempWindow subviews]) {
    if ([[keyboard description] hasPrefix : @"<UIInputSetContainerView"]) {
    for(int i = 0 ; i < [keyboard.subviews count] ; i++)
       {
        UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
        if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
            [hostkeyboard addSubview:extraRow];
            [hostkeyboard bringSubviewToFront:extraRow];
           }
       }
    }
}

extraRow 是由按钮组成的 UIView。

有什么遗漏吗?

【问题讨论】:

  • 为什么不使用UITextField's 和UITextView's inputAccessoryViewinputView 属性?这似乎是错误的,并且(正如您所发现的)很容易被破坏。想知道为什么这甚至可以这样做。
  • @JanGreve 希望将视图作为键盘上方的叠加层,并在需要时将其删除,以保持键盘完好无损。有什么方法可以抑制下面的内容并优先考虑上面的观点?感谢您的回复。
  • 键盘不被视为您应用程序的一部分;我怀疑是否有合法的方法(w.r.t. Apples 规则)这样做。

标签: ios custom-keyboard ios9.2


【解决方案1】:

您可以将自定义视图作为 inputAccessoryView 添加到键盘。然后它只是为按钮分配你希望它在点击时触发的正确目标

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    UIView  * theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [theView setBackgroundColor: [UIColor greyColor]];

    UITextField * txtTest = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, theView.frame.size.width, 60)];
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 65, 100, 30)];

    [btn setTitle:@"Click my Button" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(alert:) forControlEvents:UIControlEventTouchUpInside];

    // Just put this to see the items or customize the color
    [txtTest setBackgroundColor:[UIColor whiteColor]];
    [btn setBackgroundColor:[UIColor blueColor]];

    // Need this to add it to the view
    [theView addSubview:txtTest];
    [theView addSubview:btn];

    textField.inputAccessoryView = theView;
}

【讨论】:

    【解决方案2】:

    您可能要考虑的一件事是将extraRow 的exclusiveTouch 属性设置为YES。作为免责声明,我自己没有尝试过这个示例,但我认为您遇到的问题是由于沿触摸事件传递的视图。

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多