2014-09-25 60 views
5

完成按鈕我使用下面的代碼從視圖&添加完成按鈕鍵盤的位置上它。但是在IOS 8是無法得到的鍵盤位置&因此不添加完成按鈕。如何讓鍵盤位置的ios 8加上數字小

UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 

UIView *keyboard; 

for (int i = 0; i < [tempWindow.subviews count]; i++) 
{ 
    keyboard = [tempWindow.subviews objectAtIndex:i]; 
    // keyboard view found; add the custom button to it 

    if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) 
    { 
     [keyboard addSubview:doneButton]; 
    } 
} 

回答

13

在下面的代碼是用於示出在數字小iOS 8的「DONE」按鈕。 我在XCode-5.1.1中運行此代碼,使用iOS 6/7/8設備。它的工作完美。

我從這個鏈接Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad的參考資料給出了一些編號鍵盤上的添加按鈕的代碼。

@property (nonatomic, retain) UIButton *doneButton; 

Add按鈕

- (void)addButtonToKeyboard 
{ 
    if (!self.doneButton) 
    { 
     self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [self.doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    self.doneButton.adjustsImageWhenHighlighted = NO; 
    [self.doneButton setTitle:@"DONE" forState:UIControlStateNormal]; 
    [self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]]; 
    [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 

    // locate keyboard view 
    if ([[[UIApplication sharedApplication] windows] count] <= 1) return; 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews count]; i++) 
    { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 
     // keyboard found, add the button 
     if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
     { 
      BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation); 
      self.doneButton.frame = CGRectMake(((isPortrait)?0:-1),((int) (keyboard.frame.size.height*3)/4) + ((isPortrait)?0:1),(int) keyboard.frame.size.width/3-1, (isPortrait)?60:40); 
      [keyboard addSubview:self.doneButton]; 
     } 
     //This code will work on iOS 8.0 
     else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES) 
     { 
      for(int i = 0 ; i < [keyboard.subviews count] ; i++) 
      { 
       UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i]; 
       if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES) 
       { 
        BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation); 
        self.doneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 60 : 40); 
        [hostkeyboard addSubview:self.doneButton]; 
       } 
      } 
     } 
     else{} 
    } 
} 

removeButtonFromKeyboard

- (void)removeButtonFromKeyboard 
{ 
    NSArray *arTemp = [[UIApplication sharedApplication] windows]; 
    if ([arTemp count] <= 1) return; 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews count]; i++) 
    { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 
     // keyboard found, add the button 
     if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
     { 
      for (id temp in keyboard.subviews) 
      { 
       if ([temp isKindOfClass:[UIButton class]]) 
       { 
        UIButton *btnDone = (UIButton*) temp; 
        [btnDone removeFromSuperview]; 
        break; 
       } 
      } 
     } 
     //This code will work on iOS 8.0 
     else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES) 
     { 
      for(int i = 0 ; i < [keyboard.subviews count] ; i++) 
      { 
       UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i]; 
       if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES) 
       { 
        for (id temp in hostkeyboard.subviews) 
        { 
         if ([temp isKindOfClass:[UIButton class]]) 
         { 
          UIButton *btnDone = (UIButton*) temp; 
          [btnDone removeFromSuperview]; 
          break; 
         } 
        } 
       } 
      } 
     } 
     else{} 
    } 
} 

讓我知道的任何問題。

更新: 在iOS 7.1,真實設備上測試 - 除非鍵盤顯示動畫已完成,否則不會添加按鈕。下面的代碼添加延遲添加按鈕一次鍵盤是完全可見:

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self performSelector:@selector(addButtonToKeyboard) withObject:nil afterDelay:0.75]; 

} 
+0

感謝ü它爲我工作 – poojathorat 2014-09-26 05:53:23

+0

GR8它的工作,但是當我改變文本框從數字鍵盤普通鍵盤完成按鈕出現在keyboard..what面前我應該怎麼辦? – iAnurag 2014-09-26 13:35:45

+0

你要叫removeButtonFromKeyboard在「textFieldShouldEndEditing」委託方法。而已。 – alishaik786 2014-09-26 15:07:30

0
  • 我不喜歡「亞歷克斯石」但我不能點擊自定義按鈕。

  • 我的解決辦法:

//創建DoneCustomReturnKeyButton添加到鍵盤

- (void)addDoneCustomReturnKeyButtonInKeyboard:(NSNotification *)notification 
{ 
NSArray *arr = [[[[UIApplication sharedApplication] windows] lastObject] subviews]; 
if (arr.count > 0) { 

    UIView *keyboardView = [arr objectAtIndex:0]; 
    float height; 
    if ([[keyboardView description] hasPrefix:@"<UIPeripheralHost"] == YES) { 
     height = (CGRectGetHeight(keyboardView.frame) - 
        CGRectGetHeight(self.navigationController.navigationBar.frame))/4; 

     [self.doneCustomReturnKeyButton setFrame:CGRectMake(0, keyboardView.frame.size.height - height, 
                  keyboardView.frame.size.width/3 - 2, height)]; 
     [keyboardView addSubview:self.doneCustomReturnKeyButton]; 
    } 
    //This code will work on iOS 8.0 
    else if([[keyboardView description] hasPrefix:@"<UIInputSetContainerView"] == YES) 
    { 
     for(int i = 0 ; i < [keyboardView.subviews count] ; i++) 
     { 
      UIView* hostkeyboard = [keyboardView.subviews objectAtIndex:i]; 
      if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES) 
      { 
       UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123]; 
       if (donebtn == nil) 
       { 
        height = (CGRectGetHeight(hostkeyboard.frame) - 
           CGRectGetHeight(self.navigationController.navigationBar.frame))/4; 

        [self.doneCustomReturnKeyButton setFrame:CGRectMake(0, keyboardView.frame.size.height - height, 
                     keyboardView.frame.size.width/3 - 2, height)]; 

        [keyboardView addSubview:self.doneCustomReturnKeyButton]; 
       } 
      } 
     } 
    } 
} 
else { 
    self.doneCustomReturnKeyButton.hidden = YES; 
} 

}

,並創建按鈕。

- (void)createDoneCustomReturnKeyButton 
{ 
    self.doneCustomReturnKeyButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [self.doneCustomReturnKeyButton setTitle:NSLocalizedString(@"NEXT", nil) forState:UIControlStateNormal]; 
    [self.doneCustomReturnKeyButton.titleLabel setFont:[UIFont systemFontOfSize:20]]; 
    self.doneCustomReturnKeyButton.adjustsImageWhenHighlighted = NO; 
    self.doneCustomReturnKeyButton.backgroundColor = [UIColor lightGrayColor]; 
    [self.doneCustomReturnKeyButton setTintColor:[UIColor whiteColor]]; 
    [self.doneCustomReturnKeyButton addTarget:self 
             action:@selector(doneCustomReturnKeyButtonAction:) 
          forControlEvents:UIControlEventTouchUpInside]; 
} 
0

好這裏是在這兩個iOS版9入門「完成」按鈕來顯示和工作在我的應用程序的簡單修復,iOS的8以下,當我得到了類似的錯誤。在運行應用程序並通過'View's Hierarchy'查看它後可以觀察到(例如,在應用程序運行於設備並檢查Storyboard中的視圖時,單擊調試區域欄中的'View Hierarchy'圖標),鍵盤是在iOS 9的不同窗口中呈現,與iOS 8及以下版本相比,必須予以考慮。 addButtonToKeyboard

- (id)addButtonToKeyboard 
{ 
if (!doneButton) 
{ 
    // create custom button 
    UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(-2, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; 
    [doneButton addTarget:self action:@selector(saveNewLead:) forControlEvents:UIControlEventTouchUpInside]; 
} 

NSArray *windows = [[UIApplication sharedApplication] windows]; 
//Check to see if running below iOS 9,then return the second window which bears the keyboard 
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) { 
return windows[windows.count - 2]; 
} 
else { 
UIWindow* keyboardWithDoneButtonWindow = [ windows lastObject]; 
return keyboardWithDoneButtonWindow; 
    } 
} 

這是你可以removeKeyboardButton從鍵盤如何,如果你想要的。

- (void)removeKeyboardButton { 

id windowTemp = [self addButtonToKeyboard]; 

if (windowTemp) { 

    for (UIView *doneButton in [windowTemp subviews]) { 
     if ([doneButton isKindOfClass:[UIButton class]]) { 
      [doneButton setHidden:TRUE]; 
     } 
    } 
    } 
} 

如果您在使用UITextfieldsDelegeates方法,你可以簡單地調用在textFieldDidBeginEditing委託方法的addButtonToKeyboard方法和應該這樣做。現在,如果你來回切換數字小和默認鍵盤之間,建議你撥打removeKeyboardButton在「textFieldShouldEndEditing」委託方法,以防止任何事故。