2011-11-14 15 views
0

私はテキストフィールドがあるアプリケーションを持っています。それをクリックすると画面がスクロールし、キーボードが表示されます。私はこのコードを使用します。キーボードに完了ボタンを追加するにはどうすればいいですか?

- (void) viewWillAppear:(BOOL)animated { 

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [self willAnimateRotationToInterfaceOrientation:orientation duration:0]; 

    [super viewWillAppear:animated]; 
    NSLog(@"Registering for keyboard events"); 

    // Register for the events 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil]; 

    // Setup content size 
    scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT); 

    //Initially the keyboard is hidden 
    keyboardVisible = NO; 
} 

- (void)keyboardWillShow:(NSNotification *)note { 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for (keyboard in tempWindow.subviews) { 
     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
      if (numberPadShowing) { 
       [self addButtonToKeyboard]; 
       return;     
       break; 
      } else { 
       for (UIView *v in [keyboard subviews]){ 
        if ([v tag]==123) 
         [v removeFromSuperview]; 
       } 
      } 
    } 
} 

-(void) keyboardDidShow: (NSNotification *)notif { 
    NSLog(@"Keyboard is visible"); 
    // If keyboard is visible, return 
    if (keyboardVisible) 
    { 
     NSLog(@"Keyboard is already visible. Ignore notification."); 
     return; 
    } 

// if clause is just an additional precaution, you could also dismiss it 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
     [self addButtonToKeyboard]; 
    }  


    // Get the size of the keyboard. 
    NSDictionary* info = [notif userInfo]; 
    NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGSize keyboardSize = [aValue CGRectValue].size; 

    // Save the current location so we can restore 
    // when keyboard is dismissed 
    offset = scrollView.contentOffset; 

    // Resize the scroll view to make room for the keyboard 
    CGRect viewFrame = scrollView.frame; 
    viewFrame.size.height -= keyboardSize.height; 
    scrollView.frame = viewFrame; 
    UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation]; 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     CGRect textFieldRect = [myActiveTextField frame]; 
     textFieldRect.origin.y += 20; 
     [scrollView scrollRectToVisible:textFieldRect animated:YES]; 
    } else { 
     CGRect textFieldRect = [myActiveTextField frame]; 
     textFieldRect.origin.y += 80; 
     [scrollView scrollRectToVisible:textFieldRect animated:YES]; 

    } 
    NSLog(@"ao fim"); 
    // Keyboard is now visible 
    keyboardVisible = YES; 
} 

-(void) keyboardDidHide: (NSNotification *)notif { 
    // Is the keyboard already shown 
    if (!keyboardVisible) { 
     NSLog(@"Keyboard is already hidden. Ignore notification."); 
     return; 
    } 

    // Reset the frame scroll view to its original value 
    scrollView.frame = CGRectMake(0, 44, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT); 

    // Reset the scrollview to previous location 
    scrollView.contentOffset = offset; 

    // Keyboard is no longer visible 
    keyboardVisible = NO; 
} 


- (void)addButtonToKeyboard { 
    // create custom button 
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation]; 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     doneButton.frame = CGRectMake(0, 260, 160, 40); 
    } else { 
     doneButton.frame = CGRectMake(0, 163, 106, 53); 
    } 
    doneButton.adjustsImageWhenHighlighted = NO; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { 
     [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted]; 
    } else {   
     [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; 
    } 
    [doneButton addTarget:self action:@selector(doneButton) forControlEvents:UIControlEventTouchUpInside]; 
    // locate keyboard view 
    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 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
       [keyboard addSubview:doneButton]; 
     } else { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
    } 
} 

今の問題は、それがポートレートモードで正常に動作するということですが、私はテキストフィールドをクリックするとランドスケープモードでテキストフィールドがあることことをそんなにキーボードが表示され、表示がスクロールします編集した画像は画面に表示されなくなります。また、数字キーボード(Phone.appにあるようなもの)には、横向きモードのカスタム追加ボタンはありませんが、縦長モードのものがあります。
これを修正するにはどうすればよいですか?

+0

英語を改善してください...あなたの質問に適切な文法の構文がありません。この読者のためにあなたが聞きたいものを得ることができないかもしれません... – Minakshi

+0

キーボードはそれ自身の完了キーを持っていませんか? Interface Builderで設定できると思ったのですか? @Abcは、彼のために、少なくとも部分的に –

答えて

0

Iは、風景モードで追加行わボタン

- (void)addButtonToKeyboard { 
// create custom button 
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation]; 
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
{ 
    doneButton.frame = CGRectMake(0, 123, 160, 39); 

} 
else{ 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
} 
doneButton.adjustsImageWhenHighlighted = NO; 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { 
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted]; 
} else {   
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; 
} 
[doneButton addTarget:self action:@selector(doneButton) forControlEvents:UIControlEventTouchUpInside]; 
// locate keyboard view 
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 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
     if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
      [keyboard addSubview:doneButton]; 
    } else { 
     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
      [keyboard addSubview:doneButton]; 
    } 
} 
    } 

この方法は、数字パッドのためのモードの両方のためのものであるため、このコードを使用します。

関連する問題