2016-02-01 29 views
-1

已添加由鍵盤頂部的按鈕組成的自定義視圖。按鈕正在正確顯示,但按下按鈕時,鍵盤的底層按鍵被按下而不是按鈕操作。鍵盤上方的自定義視圖無法在ios9中工作

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。

有什麼遺漏嗎?

+1

爲什麼不使用'UITextField'和'UITextView'的'inputAccessoryView'或'inputView'屬性?這似乎是錯誤的,並且(如你所發現的)很容易被破壞。想知道爲什麼這甚至可以做。 – SmokeDispenser

+0

@JanGreve想要將視圖作爲鍵盤上方的疊加層,並在需要時將鍵盤保持原樣移除。有什麼方法可以壓制下面的觀點並優先考慮上述觀點? 謝謝你的迴應。 – jeff

+0

鍵盤不被視爲您的應用程序的一部分;我懷疑是否有合法的方式(蘋果規則)可以做到這一點。 – SmokeDispenser

回答

0

您可以將一個自定義視圖添加到鍵盤作爲inputAccessoryView。然後,它只是分配按鈕,你想你可能要考慮的是extraRow的exclusiveTouch屬性設置爲YES

- (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; 
} 
0

一件事點擊的時候火正確的目標。作爲一個免責聲明,我自己並沒有嘗試過這個例子,但我認爲你遇到的問題是由於沿着觸摸事件傳遞的視圖。

相關問題